How can I send mail from an iPhone application How can I send mail from an iPhone application ios ios

How can I send mail from an iPhone application


On iOS 3.0 and later you should use the MFMailComposeViewController class, and the MFMailComposeViewControllerDelegate protocol, that is tucked away in the MessageUI framework.

First add the framework and import:

#import <MessageUI/MFMailComposeViewController.h>

Then, to send a message:

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];controller.mailComposeDelegate = self;[controller setSubject:@"My Subject"];[controller setMessageBody:@"Hello there." isHTML:NO]; if (controller) [self presentModalViewController:controller animated:YES];[controller release];

Then the user does the work and you get the delegate callback in time:

- (void)mailComposeController:(MFMailComposeViewController*)controller            didFinishWithResult:(MFMailComposeResult)result                         error:(NSError*)error;{  if (result == MFMailComposeResultSent) {    NSLog(@"It's away!");  }  [self dismissModalViewControllerAnimated:YES];}

Remember to check if the device is configured for sending email:

if ([MFMailComposeViewController canSendMail]) {  // Show the composer} else {  // Handle the error}


MFMailComposeViewController is the way to go after the release of iPhone OS 3.0 software. You can look at the sample code or the tutorial I wrote.


A few things I'd like to add here:

  1. Using the mailto URL won't work in the simulator as mail.app isn't installed on the simulator. It does work on device though.

  2. There is a limit to the length of the mailto URL. If the URL is larger than 4096 characters, mail.app won't launch.

  3. There is a new class in OS 3.0 that lets you send an e-mail without leaving your app. See the class MFMailComposeViewController.