Saturday, June 1, 2013

Adding Email functionality to a Monotouch DialogViewController.

RootElement re = new RootElement("Example");
Section Email = new Section("Email");
if (MFMailComposeViewController.CanSendMail) 
    Email.Add(new ImageStringElement("Email",delegate { 

public static void EmailFile(string path)
            BTProgressHUD.Show ("Loading. . .",-1,BTProgressHUD.MaskType.Black);
            BTProgressHUD.InvokeInBackground (delegate {
            UIApplication.SharedApplication.Windows[0].InvokeOnMainThread(delegate {
            MFMailComposeViewController mail = new MFMailComposeViewController ();
            mail.MailComposeDelegate = new CustomMailComposeDelegate ();

public class CustomMailComposeDelegate : MFMailComposeViewControllerDelegate
    public override void Finished (MFMailComposeViewController controller,
                                   MFMailComposeResult result, NSError error)
        controller.DismissViewController (true, null);

My only note on this, is that you'll need a consistent way to determine mimetype.

Behavior for the OS shifts based on Mimetype.  Example, if the file is a picture, the mail controller will load the image within the email, so that you can see it.  There could be other mimetypes it has special behavior for at some point.  The mimetype that is sent doesn't really matter to the mail controller, for the most part.

You do however really need the "CanSendMail" statement, which tells you whether someone has setup an email account on their device, whether IPhone or IPad.

You only want the Email option to show if they have configured their email accounts.  Otherwise serious breakage occurs.

: )