Sending mail with MFMailComposeViewController

Posted by Daniel Vela on February 28, 2011

First include MessageUI.framework an implement MFMailComposeViewControllerDelegate.

Implement a method like this one an the delegate method for removing the mail controller.

- (IBAction)pressentMailController:(id)sender {
 MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
 picker.mailComposeDelegate = self;
 [picker setSubject:@"Test subject!"];
 // Set up the recipients.
 /*NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil];
 NSArray *ccRecipients = [NSArray arrayWithObjects:@"", @"", nil];
 NSArray *bccRecipients = [NSArray arrayWithObjects:@"", nil];
 [picker setToRecipients:toRecipients];
 [picker setCcRecipients:ccRecipients];
 [picker setBccRecipients:bccRecipients];
 // Attach an image to the email.
 /*NSString *path = [[NSBundle mainBundle] pathForResource:@"ipodnano"
 NSData *myData = [NSData dataWithContentsOfFile:path];
 [picker addAttachmentData:myData mimeType:@"image/png"
 // Fill out the email body text.
 NSString *emailBody = @"It is raining in sunny California!";
 [picker setMessageBody:emailBody isHTML:NO];
 // Present the mail composition interface.
 [self presentModalViewController:picker animated:YES];
 [picker release]; // Can safely release the controller now.
 // The mail compose view controller delegate method
 - (void)mailComposeController:(MFMailComposeViewController *)controller
 error:(NSError *)error
 [self dismissModalViewControllerAnimated:YES];

The code allows to add recipients, body, subject and attachements. Uncomment the lines as needed.

mail controller