Send email from an App

In this tutorial, I will show how to allow users to send an e-mail without leaving your app using the MFMailComposeViewController class. We will setup an e-mail template with a subject and a body text. To edit and send an email from your app you will need to use the MFMailComposeViewController class. This view controller displays a standard mail interface and also provides the functionality to respond to user events performed on that interface.

Update: Dec 15, 2014. The rewritten version in Swift for iOS 8.1 and Xcode 6.1 is available here.

Create a new Project. Select the Single View Application. Name the project EmailDemo. Check the Use Automatic Reference Counting. Build for the Iphone Device. This project is created with Xcode 4.5 and built for iOS 6.

First create the properties. In ViewController.m add the following lines

@property (nonatomic, weak) IBOutlet UITextField *subject;
@property (nonatomic, weak) IBOutlet UITextView *messageBody;
@property (nonatomic, weak) IBOutlet UIButton *mailButton;

Also we create the delegate protocols for the MFMailComposeViewController, UITextField and the UITextView. In ViewController.m. Change the @interface line in

@interface ViewController () <MFMailComposeViewControllerDelegate, UITextFieldDelegate, UITextViewDelegate> 

Create the following stub method. We will connect the Button to this method.

- (IBAction)sendMail {


Open ViewController.xib. Drag a TextField, a TextView and a Button to the view. The view will look like this

Make the following connections in IBBuilder.

  • TextField -> IBOutlet subject
  • TextView -> IBOutlet messageBody
  • Button -> IBOutlet mailButton
  • Button -> IBAction sendMail

Now the connections are made we go back to ViewController.m. In ViewDidLoad set the delegates to our ViewController(self)

- (void)viewDidLoad 
[super viewDidLoad];

self.subject.delegate = self;
self.messageBody.delegate = self;

Now let's implement the sendMail method. First create a MFMailComposeViewController object and point the delegate to our ViewController.


- (IBAction)sendMail 
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

Next point the text from the user input from our TextField/TextView to the setSubject/setMessageBody method of the MFMailComposeViewController.

[picker setSubject:self.subject.text]; 
[picker setMessageBody:self.messageBody.text isHTML:YES];

Change the style of the navigationbar and present the mail composer view

picker.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[self presentViewController:picker animated:YES completion:NULL];

Ok. the only thing there's left to do is to implement the delegate methods. First we dismiss the Mail Composer ViewController in the mailComposeController:didFinishWithResult:error method.

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
[self dismissViewControllerAnimated:YES completion:NULL];

In our current setup, when the user is finished inputting text in the UITextField, the keyboard can't be dismissed. In the textFieldShouldReturn method we resign The FirstResponder so the control is given back to the ViewController.

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
[textField resignFirstResponder];
return YES;

Almost the same story applies for the TextViewDelegate method, except here we dismiss the keyboard when the user pressed enter.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
self.messageBody.text = textView.text;
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;

return YES;

Ok, all the code is completed. Build and Run. Fill in the fields and press the Email Button.

The MFComposerView is loaded and the subject and message fields are already filled in.Note that in the simulator you can't send the email. This can only be done on a real device.