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.
Any idea how to implement “Save as Draft” ?
Awesome just what I was looking for cheers!
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!
Hey check this out. It will help you.
http://stackoverflow.com/questions/4749681/monotouch-how-to-capture-the-cancel-button-when-sending-email
Hi,
I wanna make pdf from the view controller, Could you help me?
Take a look at this sample from chapter 6 of my book: https://github.com/mikebluestein/learning_monotouch_code/tree/master/ch6/LMT6-3/LMT6-3
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.