To Send Email from iOS Application you must have to import MessageUI Framework to your application.
To add this Framework Select -> Target -> Build Phases -> Link Binary with Libraries
Next, click "+" button and select "MessageUI Framework" and click the Add button. After you click the Add button Framework will be added to your application.
Now import Framework in your file.
#import <MessageUI/MessageUI.h>
Add MFMailComposeDelegate
@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate
Now write below code to send mail from our app. here I have added one button named "Send Email"and connected my below method to it to send the mail. You can put this code from where ever you wants to send mail.
- (IBAction)sendEmail:(id)sender
{
// Email Subject
NSString *emailTitle = @"Test mail";
// Email Content
NSString *messageBody = @"It is Mail for testing!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"ios@gmail.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
This is Delegate method of MFMailComposeViewController. This method will be called automatically when the mail interface is closed. (e.g. user cancel the operation or mail sent)
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
Build and Run and you will see just like that
Compose HTML mail if you wants. then you just have to make only one change. set isHTML to YES message body
[mc setMessageBody:messageBody isHTML:YES];
And write HTML Tags to your body just like that.
NSString *messageBody = @"<h1>Learning iOS Programming!</h1>";
Thanks & Regards
Mohit Popat


