Send email from your App Part 1 This is a short and simple tutorial that will demonstrate how to develop an app that sends an email from within the app. Step 1: Create a Single View Application and name it SendMail as follows: (Note: You can use the Story board if you want but this is just a single view application.) Step 2: The first thing we will have to do is add the MessageUI.framework to our project. Right click on the main project folder at the top of the Project Explorer window. Click on the Build Phases tab on the top and open up the Link Binary With Libraries category.
Click on the very little + (plus symbol) at the bottom of the category window. Find the MessageUi.framework and click add.
The finished list looks like this: Step 4: Create the User Interface to look like this. Basically add 3 Text Fields and 1 Button so it looks something like this: Step 4: Open ViewController.h. First we need to import the MessageUI class by adding the header to the top of ViewController.h as follows: #import <MessageUI/MessageUI.h> We also need to add the <MFMailComposeViewControllerDelegate> protocol, like this:
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate> Now we need to wire the button and the three Text Fields as follows: @property (weak, nonatomic) IBOutlet UITextField *to; @property (weak, nonatomic) IBOutlet UITextField *subject; @property (weak, nonatomic) IBOutlet UITextField *message; - (IBAction)send:(UIButton *)sender; We can now add a method to get rid of the keyboard when the user is done typing into each of the Text Fields. Wire one of the Text Field s, Did End on Exit to an IBOutlet called finished. The following method looks like this: - (IBAction)finished:(UITextField *)sender; Wire the other two Did End on Exit events to this method as well so all three Text Fields will work with this one method. After doing this you can save and close this file. Step 5: Open ViewController.m Add the line to synthesize the properties as follows: @synthesize to, subject, message; Step 6: Implement the reignfirstresponder for the text fields. Add the code to the finished method to clean up our UI after the user is done typing into the text fields. This doesn t have anything to do with emailing it s just used to close the keyboard after a user enters text into a Text Field. - (IBAction)finished:(UITextField *)sender [sender resignfirstresponder]; Step 7: Add the send method implementation.
All the work is done by the MFMailComposeViewController. First we check to see if the phone can send mail. If it can we create an instance of the MFMailComposeViewController and use it to set the subject, the body and the recipients of the email. settorecipients takes an array as an argument so we need to put our to text into an array. After setting up the email we present the email view the present ModalViewController. The finished method looks like this: - (IBAction)send:(UIButton *)sender NSLog(@"Inside send method"); if ([MFMailComposeViewController cansendmail]) // set the send To address NSMutableArray *recipients = [[NSMutableArray alloc] initwithcapacity:1]; [recipients addobject:to.text]; MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; controller.mailcomposedelegate = self; [controller setsubject:subject.text]; [controller setmessagebody:message.text ishtml:no]; [controller settorecipients:recipients]; [self presentmodalviewcontroller:controller animated:yes]; else UIAlertView *alert = [[UIAlertView alloc] initwithtitle:@"alert" message:@"your device is not set up for email." delegate:self cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alert show];
Step 8: In order to get rid of the email view after sending we need to implement the mailcomposecontroller:didfinishwithresult:error method. In this method we can handle if the user selected to cancel sending the email and decided to delete or save it. We won t do anything but send an alert in the event that the email failed after the user tried to send it. Add the following method to ViewController.m: - (void)mailcomposecontroller:(mfmailcomposeviewcontroller*)contr oller didfinishwithresult:(mfmailcomposeresult)result error:(nserror*)error switch (result) case MFMailComposeResultCancelled: case MFMailComposeResultSaved: case MFMailComposeResultSent: case MFMailComposeResultFailed: default: UIAlertView *alert = [[UIAlertView alloc] initwithtitle:@"email" message:@"email Failed" delegate:self cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alert show];
[self becomefirstresponder]; [self dismissmodalviewcontrolleranimated:yes]; Step 9: Build and Run and check it out. Of course the simulator isn t going to be able to send a real email. You ll have to run it on a phone to do that. You should see the following results: When you press the Send Email button you will see: After you press Send you will be returned back to your app.