Mike's Dev Blog

Sending Mail from an iPhone App with Monotouch

Posted in monotouch by mikebluestein on December 11, 2009

You can send email on the iPhone from within your application using the MFMailComposeViewController in the MonoTouch.MessageUI namespace. Simply check if the device is able to send mail using the CanSendMail property and if it is true, bring up an MFMailComposeViewController. The controller has properties to add attachments (using the AddAttachmentData method), set the message body, send html mail, add cc recipients, etc. After hydrating your controller and presenting it, you can listen for completion results by subscribing to the Finished event (or you could do it the Delegate way and override the Finished virtual function on MFMailComposeViewControllerDelegate as well). In the callback you get back a result object, an error object and the controller itself via the MFComposeResultEventArgs, which you can use to present the completion status and dismiss the controller. Here’s a simple example:

        MFMailComposeViewController _mail;
        ...

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            
            mailButton.TouchUpInside += (o, e) =>
            {
                if (MFMailComposeViewController.CanSendMail) {
                    _mail = new MFMailComposeViewController ();
                    _mail.SetMessageBody ("This is the body of the email", 
                        false);
                    _mail.Finished += HandleMailFinished;
                    
                    this.PresentModalViewController (_mail, true);
                    
                } else {
                    // handle not being able to send mail
                }
            };
        }

        void HandleMailFinished (object sender, MFComposeResultEventArgs e)
        {
            if (e.Result == MFMailComposeResult.Sent) {
                UIAlertView alert = new UIAlertView ("Mail Alert", "Mail Sent",
                    null, "Yippie", null);
                alert.Show ();
                
                // you should handle other values that could be returned 
                // in e.Result and also in e.Error 
            }
            e.Controller.DismissModalViewControllerAnimated (true);
        }

Update: Added link to the sample project here.

About these ads

8 Responses

Subscribe to comments with RSS.

  1. MonoTouch.Info said, on December 11, 2009 at 10:19 am

    MonoTouch Article – Sending Mail from an iPhone App with Monotouch…

    Thank you for submitting this entry – Trackback from MonoTouch.Info…

  2. Monkey said, on July 14, 2010 at 6:41 pm

    Any idea how to implement “Save as Draft” ?

  3. Stuart Clark said, on July 20, 2010 at 8:40 am

    Awesome just what I was looking for cheers!

  4. Christian Weyer said, on August 9, 2010 at 7:04 pm

    Hey Mike,

    did you successfully handle the Cancel action? E.g. when dismissing the draft. It does not jump into the Finished handler for me…
    Thanks!

  5. Pourang Shakib said, on October 12, 2011 at 4:08 pm

    Hi,

    I wanna make pdf from the view controller, Could you help me?

  6. Anup Marwadi said, on June 6, 2012 at 8:15 pm

    Hello, would love to know if there’s a way to set the Cursor position to a line in the message field in the Mail Composer? Any help would be appreciated.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.