Sending an email from an iOS FireMonkey application

Posted by on in Blogs
Q: How do you send an email from the iPhone using FireMonkey?
A: By opening a mailto URL

Below is a very simple unit that will do the trick. Just create a brand new iOS FireMonkey application, and add two edit boxes and one memo. The first edit box will hold the email address we're sending the email to. The second edit box will contain the subject line, and the memo will contain the actual email body.

Notice that this opens the local email client and you have to send it manually. It does *not* send the email in the background.

Enjoy!

unit Unit1;

{$IFDEF FPC}
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$ENDIF}

interface

uses
SysUtils, Types, UITypes, Classes, Variants, FMX_Types, FMX_Controls, FMX_Forms,
FMX_Dialogs, FMX_Layouts, FMX_Memo, FMX_Edit
{$IFDEF FPC}
, iPhoneAll
{$ENDIF}
;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.Button1Click(Sender: TObject);
var
ToStr, SubjectStr, BodyStr : String;
URL : NSString;
begin
{$IFDEF FPC}

ToStr := Edit1.Text;
SubjectStr := Edit2.Text;
BodyStr := Memo1.Text;

URL := NSSTR(PChar('mailto:'+ToStr+'?subject='+SubjectStr+'&body='+BodyStr));
URL := URL.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding);

UIApplication.sharedApplication.openUrl(NSURL.URLWithString(URL));
{$ENDIF}
end;

end.





Comments

  • Guest
    Jörg Sunday, 3 June 2012

    Hello,
    I've tried the example and sometimes it does not work. The fault must lie here on this line:

    URL: = URL.stringByAddingPercentEscapesUsingEncoding (NSUTF8StringEncoding);

    UIApplication.sharedApplication.openUrl (NSURL.URLWithString (URL));

    If Xcode launches the iPhone simulator, it reports these lines, that something is wrong.

    Can you help me?

  • Guest
    Anders Ohlsson Monday, 4 June 2012

    That's correct. The Mail app doesn't exist on the simulator. This demo only works on a device that has at least one email account setup in the Mail app.

  • Guest
    Jörg Tuesday, 5 June 2012

    Even if I want to transfer the app from Xcode on the device (iphone) Xcode brings me out of error messages.
    In the uses - is part involved iphoneall yes, I put the app into iphoneall.pas directory he wants all * inc files, if I leave out my Xcode also brings the error messages..

  • Guest
    Anders Ohlsson Tuesday, 5 June 2012

    I'm afraid I don't understand what you're doing. If you installed FreePascal and FireMonkey on the Mac from the FireMonkey_iOS.dmg file, everything should just work. iPhoneAll.pas is part of FPC and so are all the *.inc files.

    Put your own app in your own directory. Do not put it in the directory where iPhoneAll.pas is located.

    Better yet, simply use my set of components located here:

    http://cc.embarcadero.com/Item/28635

    FMX_Email is the demo you would use for sending emails.

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: