<!--
{
  "availability" : [
    "iOS: 3.0.0 -",
    "iPadOS: 3.0.0 -",
    "macCatalyst: 13.1.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "MessageUI",
  "identifier" : "/documentation/MessageUI/MFMailComposeViewController",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Message UI"
    ],
    "preciseIdentifier" : "c:objc(cs)MFMailComposeViewController"
  },
  "title" : "MFMailComposeViewController"
}
-->

# MFMailComposeViewController

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

```
@MainActor class MFMailComposeViewController
```

## Overview

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 person can edit your initial values before sending the email.

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

![Screenshot of the email composition view in Mail, indicating the fields for recipients, subject, and body. ](images/com.apple.messageui/media-4288076@2x.png)

> Important:
> You must not modify the view hierarchy presented by this view controller. However, you can customize the appearance of the interface using the <doc://com.apple.documentation/documentation/UIKit/UIAppearance> protocol.

An alternate way to compose emails is to create and open a URL that uses the `mailto` scheme. URLs of that type go directly 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](https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007899).

### Checking the availability of the composition interface

Before presenting the mail compose view controller, always call the [`canSendMail()`](/documentation/MessageUI/MFMailComposeViewController/canSendMail()) method to see if the person configured the current device to send email. If the person’s device isn’t set up for the delivery of email, you can notify the person or disable the email dispatch features in your application. You shouldn’t attempt to use this interface if the [`canSendMail()`](/documentation/MessageUI/MFMailComposeViewController/canSendMail()) method returns <doc://com.apple.documentation/documentation/Swift/false>.

```swift
if !MFMailComposeViewController.canSendMail() {
    print("Mail services are not available")
    return
}
```

### 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 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. The sample code below shows how to configure the composition interface and present it modally. Always assign a delegate to the [`mailComposeDelegate`](/documentation/MessageUI/MFMailComposeViewController/mailComposeDelegate) property, because the delegate is responsible for dismissing the composition interface later.

```swift
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
 
// Configure the fields of the interface.
composeVC.setToRecipients(["address@example.com"])
composeVC.setSubject("Hello!")
composeVC.setMessageBody("Hello from California!", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
```

> Important:
> After presenting a mail compose view controller, the system ignores any attempts to modify the email using the methods of this class. The user can still edit the content of the email, but your app can’t. Therefore, always configure the fields of your email *before* presenting the view controller.

The mail compose view controller isn’t dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the [`mailComposeController(_:didFinishWith:error:)`](/documentation/MessageUI/MFMailComposeViewControllerDelegate/mailComposeController(_:didFinishWith:error:)) method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in sample code below. You can also use this method to check the result of the operation.

```swift
func mailComposeController(controller: MFMailComposeViewController,
                           didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    // Check the result or perform other tasks.
    
    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)
}
```

The user can delete a queued message before it’s sent. Although the view controller reports the success or failure of the operation to its delegate, this class doesn’t provide a way for you to verify if the email sent.

For more information on how to present and dismiss view controllers, see [View Controller Programming Guide for iOS](https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/index.html#//apple_ref/doc/uid/TP40007457).

## Topics

### Responding to the view controller dismissal

[`mailComposeDelegate`](/documentation/MessageUI/MFMailComposeViewController/mailComposeDelegate)

The mail composition view controller’s delegate.

[`MFMailComposeViewControllerDelegate`](/documentation/MessageUI/MFMailComposeViewControllerDelegate)

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

### Determining mail availability

[`+  canSendMail`](/documentation/MessageUI/MFMailComposeViewController/canSendMail())

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

### Setting mail fields programmatically

[`-  setSubject:`](/documentation/MessageUI/MFMailComposeViewController/setSubject(_:))

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

[`-  setToRecipients:`](/documentation/MessageUI/MFMailComposeViewController/setToRecipients(_:))

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

[`-  setCcRecipients:`](/documentation/MessageUI/MFMailComposeViewController/setCcRecipients(_:))

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

[`-  setBccRecipients:`](/documentation/MessageUI/MFMailComposeViewController/setBccRecipients(_:))

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

[`-  setMessageBody:isHTML:`](/documentation/MessageUI/MFMailComposeViewController/setMessageBody(_:isHTML:))

Sets the initial body text to include in the email.

[`-  addAttachmentData:mimeType:fileName:`](/documentation/MessageUI/MFMailComposeViewController/addAttachmentData(_:mimeType:fileName:))

Adds the specified data as an attachment to the message.

[`-  setPreferredSendingEmailAddress:`](/documentation/MessageUI/MFMailComposeViewController/setPreferredSendingEmailAddress(_:))

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

### Responding to errors

[`MFMailComposeError`](/documentation/MessageUI/MFMailComposeError)

Mail composition errors.

[`MFMailComposeErrorDomain`](/documentation/MessageUI/MFMailComposeErrorDomain)

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

[`Code`](/documentation/MessageUI/MFMailComposeError/Code)

Error codes for <doc://com.apple.documentation/documentation/Foundation/NSError> objects that are associated with the mail composition interface.

## Relationships

### Conforms To

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`UIUserActivityRestoring`](/documentation/UIKit/UIUserActivityRestoring)

[`UITraitChangeObservable-67e94`](/documentation/UIKit/UITraitChangeObservable-67e94)

[`Hashable`](/documentation/Swift/Hashable)

[`NSTouchBarProvider`](/documentation/AppKit/NSTouchBarProvider)

[`UIAppearanceContainer`](/documentation/UIKit/UIAppearanceContainer)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`UIContentContainer`](/documentation/UIKit/UIContentContainer)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`Equatable`](/documentation/Swift/Equatable)

[`UIFocusEnvironment`](/documentation/UIKit/UIFocusEnvironment)

[`UITraitEnvironment`](/documentation/UIKit/UITraitEnvironment)

[`UIPasteConfigurationSupporting`](/documentation/UIKit/UIPasteConfigurationSupporting)

[`Sendable`](/documentation/Swift/Sendable)

[`UIResponderStandardEditActions`](/documentation/UIKit/UIResponderStandardEditActions)

[`NSExtensionRequestHandling`](/documentation/Foundation/NSExtensionRequestHandling)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`UIActivityItemsConfigurationProviding`](/documentation/UIKit/UIActivityItemsConfigurationProviding)

[`UIStateRestoring`](/documentation/UIKit/UIStateRestoring)

[`CVarArg`](/documentation/Swift/CVarArg)

### Inherits From

[`UINavigationController`](/documentation/UIKit/UINavigationController)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)