A_R
A_R

Reputation: 109

iOS Firemonkey: How to send email from iOS App though mail app in Firemonkey Delphi XE7

I am working with iOS App in Firemonkey using Delphi XE7.

Question: I need to send email from my iOS App through the mail app in Firemonkey.

I have seen other old blogs for sending an email but those solutions didn't help me.

Below are the old links which I have tried it, but I couldn't solve.

http://blogs.embarcadero.com/ao/2011/10/04/39133

http://blogs.embarcadero.com/matthiaseissing/2013/05/03/38707/

Kindly let me know some other solutions or samples.

Thanks in advance.

Upvotes: 1

Views: 3081

Answers (2)

Sebastian Z
Sebastian Z

Reputation: 4740

Use the TDPFMailCompose class that is included in D.P.F Delphi iOS Native Components

That gives you more options than a mailto: link and you don't have to worry about the encoding. Internally this uses the iOS MFMailComposeViewController class.

Example:

var
  Mail: TDPFMailCompose;
begin
  Mail := TDPFMailCompose.Create(nil);
  if not Mail.MailCompose(Msg.Subject, Msg.Body, False, [Msg.To_], [Msg.CC], [Msg.BCC], [AttachedFileName]) then
     MessageDlg('Error sending mail', TMsgDlgType.mtError, [TMsgDlgBtn.mbClose], -1);
end;

Upvotes: 1

Hans
Hans

Reputation: 2262

Use the code from the second link you included: http://blogs.embarcadero.com/matthiaseissing/2013/05/03/38707/

It is for XE4 and you just need a few changes to make it work for XE7:

The StrToNSUrl function has moved to the Macapi.Helpers unit in XE7, so you must add that to your uses clause. In addition the NSStr function is deprecated and so you should use StrToNSStr instead (also from Macapi.Helpers).

Here is a function that puts all the functionality together:

procedure SendEmail(aEmail: string; aSubject: string = ''; aBody: string = '');
var lSharedApplication: UIApplication;
    lURL: string;
begin
  lURL := 'mailto:'+aEmail;
  if (aSubject<>'') or (aBody<>'') then
  begin
    lURL := lURL+'?subject='+aSubject;
    if aBody<>'' then
      lURL := lURL+'&body='+aSubject;
    lURL := StringReplace(lURL,' ','%20',[rfReplaceAll]); //replace spaces
    lURL := StringReplace(lURL,sLineBreak,'%0D%0A',[rfReplaceAll]);//replace linebreaks
  end;
  lSharedApplication := TUIApplication.Wrap(TUIApplication.OCClass.SharedApplication);
  lSharedApplication.openURL(StrToNSUrl(lURL));
end;

Call it like this:

SendEmail('[email protected]','My subject','My body');

Upvotes: 1

Related Questions