How to send an email in iOS

We can use the MFMailComposeViewController to bring up the same view controller that the Mail App uses when you write an new email. Using this method lets us populate the send-to, subject and email body.

The class part of the MessageUI framework. Using it will open the mail app on your device. Devices without mail sending capabilities will crash so we’ll check before we instantiate the controller.

Here’s how to do that:

In addition we should conform to the MFMailComposeViewController protocol, so let’s set this in our ViewController.h file. We also need to import the MessageUI Framework while we’re here:

Don’t forget to add the framework itself via Build Phases (explained in the next article).

Dismissing the view controller

The mail view controller needs to be dismissed when it’s no longer needed. The system calls the delegate method didFinishWithResult when the operation is done. At this point we can check the error object to see what went wrong – I’m just going to dismiss the controller:

Demo Project

I’ve made a small (but well commented) demo project that will demonstrate this implementation:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.