Page 1 of 1

[IOS] Send email with mailto

PostPosted: Thu Nov 01, 2018 2:08 am
by Lena
Hello. I do not have much experience with phones on the platform ios.
Please tell me where is my mistake?
1. Install application Gmail for ios on my phone from apple store.
2. Code:
Code: Select all
#include <System.Net.HttpClient.hpp>
#include <iOSapi.Foundation.hpp>
#include <Macapi.Helpers.hpp>
#include <FMX.Helpers.iOS.hpp>


bool __fastcall CheckInet2()
{
 bool result = false;
   try
   {
     THTTPClient *aHTTP = THTTPClient::Create();
     try
     {
       aHTTP->Head("http://google.com");
       result = true;
     }
     __finally {
       delete aHTTP;
     }
   }
   catch (const System::Sysutils::Exception &)
   {
   }
   return result;
}
//---------------------------------------------------------------------------

bool __fastcall OpenURL2(const String &AURL)
{

   _di_NSURL NSU = StrToNSUrl(AURL);
    if (SharedApplication()->canOpenURL(NSU))
        return SharedApplication()->openURL(NSU);
    else
      return false;

}

//---------------------------------------------------------------------------

void __fastcall TForm2::Button2Click(TObject *Sender)
{

 String SenderName = Trim(Edit1->Text);
   if (SenderName == "")
   {
      ShowMessage(L"Please enter your name.");
      Edit1->SetFocus();
      return;
   }
 bool result = CheckInet2();

   if (result == false)
   {
    ShowMessage(L"Unable to sent. Check the Internet connection.");
    return;
   }


 Button2->Enabled = false;
 Application->ProcessMessages();
   try
   {
     String ToStr = L"reviewforthc@gmail.com";
     String SubjectStr = L"Review";
     String BodyStr = L"Name: " + Trim(Edit1->Text) +
         L"; Date: " + DateTimeToStr(Calendar1->Date) +
         L"; Review: " + ComboBox1->Items->Strings[ComboBox1->ItemIndex];

     String URL = _D("mailto:") + ToStr + _D("?subject=") + SubjectStr + _D("&body=") + BodyStr;

     bool check = OpenURL2(URL);
     if(check == true)
      {
       ShowMessage(L"Thank You for review!");
      }
      else
       {
        ShowMessage(L"Sorry not found local email client.");
         }
     Close();
   }
   __finally
   {
     Button2->Enabled = true;
   }

}


I get the ShowMessage all the time Sorry not found local email client :o

Re: [IOS] Send email with mailto

PostPosted: Thu Nov 01, 2018 11:10 am
by rlebeau
Lena wrote:Please tell me where is my mistake?


You are not URL-encoding the parameter values that you pass to "mailto:" (spaces and other reserved characters must be encoded in "%HH" format, etc).

And I would suggest removing the CheckInet2() call altogether, it is not really needed on mobile. If you can't connect to an HTTP server doesn't necessary mean you can't send an email. You should display your "Unable to sent" message if OpenURL2() fails instead.

Code: Select all
#include <IdURI.hpp>

void __fastcall TForm2::Button2Click(TObject *Sender)
{
   String SenderName = Trim(Edit1->Text);
   if (SenderName.IsEmpty())
   {
      ShowMessage(_D("Please enter your name."));
      Edit1->SetFocus();
      return;
   }

   Button2->Enabled = false;
   try
   {
      String ToStr = _D("reviewforthc@gmail.com");
      String SubjectStr = _D("Review");
      String BodyStr = _D("Name: ") + Trim(Edit1->Text) +
         _D("; Date: ") + DateTimeToStr(Calendar1->Date) +
    _D("; Review: ") + ComboBox1->Items->Strings[ComboBox1->ItemIndex];

      SubjectStr = TIdURI::ParamsEncode(SubjectStr);
      BodyStr = TIdURI::ParamsEncode(BodyStr);

      //String URL = System::Sysutils::Format(_D("mailto:%s?subject=%s&body=%s"), ARRAYOFCONST(( ToStr, SubjectStr, BodyStr )));
      String URL = _D("mailto:") + ToStr + _D("?subject=") + SubjectStr + _D("&body=") + BodyStr;

      if (!OpenURL2(URL))
      {
         ShowMessage(_D("Unable to sent. Check the Internet connection, and make sure an email client is installed."));
         return;
      }

      ShowMessage(_D("Thank You for review!"));
      Close();
   }
   __finally
   {
      Button2->Enabled = true;
   }
}

Re: [IOS] Send email with mailto

PostPosted: Fri Nov 02, 2018 2:41 am
by Lena
Thank you very much! Perfect!