A standard view controller, whose interface lets the user manage, edit, and send email messages.


@interface MFMailComposeViewController : UINavigationController


Use this view controller to display a standard email interface inside your app. Before presenting the interface, populate the fields with initial values for the subject, email recipients, body text, and attachments of the email. After presenting the interface, the user can edit your initial values before sending the email.

The composition interface does not guarantee the delivery of your email message; it only lets you construct the initial message and present it for user approval. The user may opt to cancel the composition interface, in which case the message and its contents are discarded. If the user opts to send the message, the message is queued in the user’s Mail app outbox. The Mail app is ultimately responsible for sending the message.

Figure 1

The mail composition interface

Screenshot of the email composition view in Mail, indicating the fields for recipients, subject, and body.

An alternate way to compose emails is to create and open a URL that uses the mailto scheme. URLs of that type are directed to the built-in Mail app, which uses your URL to configure a message. For information about the structure of mailto URLs, see Apple URL Scheme Reference.

Checking the Availability of the Composition Interface

Before presenting the mail compose view controller, always call the the canSendMail method to see if the current device is configured to send email. If the user’s device is not set up for the delivery of email, you can notify the user or simply disable the email dispatch features in your application. You should not attempt to use this interface if the canSendMail method returns NO.

Listing 1

Checking the availability of mail services

if (![MFMailComposeViewController canSendMail]) {
   NSLog(@"Mail services are not available.");

Configuring and Displaying the Composition Interface

After verifying that mail services are available, you can create and configure the mail composition view controller and then present it as you would any other view controller. Use the methods of this class to specify the subject, recipients, and message body of the email, including any attachments you want to send with the message. Listing 2 shows how to configure the composition interface and present it modally. Always assign a delegate to the mailComposeDelegate property, because the delegate is responsible for dismissing the composition interface later.

Listing 2

Configuring and presenting the composition interface

MFMailComposeViewController* composeVC = [[MFMailComposeViewController alloc] init];
composeVC.mailComposeDelegate = self;
// Configure the fields of the interface.
[composeVC setToRecipients:@[@""]];
[composeVC setSubject:@"Hello!"];
[composeVC setMessageBody:@"Hello from California!" isHTML:NO];
// Present the view controller modally.
[self presentViewController:composeVC animated:YES completion:nil];

The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController:didFinishWithResult:error: method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in Listing 3. You can also use this method to check the result of the operation.

Listing 3

Dismissing the mail compose view controller

- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
   // Check the result or perform other tasks.
   // Dismiss the mail compose view controller.
   [self dismissViewControllerAnimated:YES completion:nil];

The user can delete a queued message before it is sent. Although the view controller reports the success or failure of the operation to its delegate, this class does not provide a way for you to verify whether the email was actually sent.

For more information on how to present and dismiss view controllers, see View Controller Programming Guide for iOS.


Responding to the View Controller Dismissal


The mail composition view controller’s delegate.


An interface for responding to user interactions with a mail compose view controller.

Determining Mail Availability

+ canSendMail

Returns a Boolean that indicates whether the current device is able to send email.

Setting Mail Fields Programmatically

- setSubject:

Sets the initial text for the subject line of the email.

- setToRecipients:

Sets the initial recipients to include in the email’s “To” field.

- setCcRecipients:

Sets the initial recipients to include in the email’s “Cc” field.

- setBccRecipients:

Sets the initial recipients to include in the email’s “Bcc” field.

- setMessageBody:isHTML:

Sets the initial body text to include in the email.

- addAttachmentData:mimeType:fileName:

Adds the specified data as an attachment to the message.

- setPreferredSendingEmailAddress:

Sets the preferred email address to use in the From field, if such an address is available.

Responding to Errors


The domain used for error objects that are associated with the mail composition interface.


Error codes for NSError objects that are associated with the mail composition interface.