This time, we’ll add in-app email to a simple iPhone application using the new-in-3.0 MessageUI framework.
Technically neither mail from an app nor MessageUI are new. MessageUI was a private framework prior to 3.0, and apps could send mail using mailto:// URLs, though in a limited way.
In any case, with 3.0 and MessageUI, sending messages is straightforward and full featured.
We’ll start with working code for a simple app with a UI containing a single button. We’ll add code that shows the compose-an-email message UI and pre-fills the subject and content when the button is pressed.
Source/GitHub
The code for this tutorial is available on GitHub. To download a copy for yourself, close the repository:
  1. Open a terminal and change to the directory where you want the code
  2. Clone the repo by typing git clone git://github.com/dcgrigsby/InAppEmail.git
I’ve made two separate commits to the repository — an initial commit without in-app email, and a second one with those capabilities added. If you’re following along step-by-step, you’ll want to revert to the earlier commit. From the source directory:
  1. Type git checkout 0019071d855e6d3a9f263b8a02d9c7d93dc5dcd2
Orientation
InAppEmail is about as simple an application as it’s possible to construct. The single “Send Email” button — which is disabled, for now — triggers the buttonPressed message when pressed.
Adding The MessageUI Framework
As part of the MessageUI Framework, Apple provides a ready-made compose-a-message UI called MFMailComposeViewController for our use. In order to use it, we need to add the MessageUI Framework to our project:
  1. In the Groups & Files panel of the project, expand the InAppEmail project branch
  2. Control-click or right-click the Frameworks folder
  3. Choose Add > Existing Frameworks…
  4. Expand the Frameworks folder
  5. Choose MessageUI.framework and click Add
  6. Click Add once more
Next, import the framework in the header:
  1. Add #import  to InAppEmailViewController.h
Showing The MFMailComposeViewController UI
MFMailComposeViewController is just like any other view controller; we can usepresentModalViewController from our InAppEmailViewController class to slide it onto the screen modally.
Replace the puttonPressed method in InAppEmailController.m with this:
- (IBAction)buttonPressed {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
[controller setSubject:@"In app email..."];
[controller setMessageBody:@"...a tutorial from mobileorchard.com" isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
}
Running the app in its current form would be disappointing. The “Send Email” message button is disabled. It’s a simple matter to enable it but, but we should do so only if the iPhone or iPod touch the app is running on has been configured to send mail. To do so, add code for aviewDidLoad method to InAppEmailViewController.m:
- (void)viewDidLoad {
if ([MFMailComposeViewController canSendMail])
button.enabled = YES;
}
Dismissing the MFMailComposeViewController UI
We’re most of the way there. Run the app at this juncture and it mostly works: clicking the button brings up the compose UI pre-filled with our sample message. Add an email address in the “To” field and away you go — mostly:
We’re missing one piece: we need to know when to dismiss the mail composer UI. TheMFMailComposeViewControllerDelegate protocol provides a callback mechanism to accomplish this.
To receive the callback, we’ll need:
  1. Adopt the protocol in our InAppEmailViewController
  2. Set ourselves up as the delegate to receive the callbacks
  3. Implement the callback method
To adopt the protocol update InAppEmailViewController.h. Changes are bold:
#import 

@interface InAppEmailViewController : UIViewController {
IBOutlet UIButton *button;
}

- (IBAction)buttonPressed;

@end
To set ourselves up as the delegate to receive the callbacks update buttonPressed in InAppEmailViewController.m. Changes are bold:
- (IBAction)buttonPressed {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"In app email..."];
[controller setMessageBody:@"...a tutorial from mobileorchard.com" isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
}
Finally, we implement the callback method. Add this code for amailComposeController:didFinishWithResult:error: method to InAppEmailViewController.m:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
Conclusion
That covers the basics. This tutorial doesn’t demonstrate setting recipients, adding attachments, or composing HTML email — all of which are straightforward once you’ve got the basics down.
Finally, one limitation that’s worth noting: you can’t force a user to send mail; they can always click the cancel button.