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
Now import Framework in your file.
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.
#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
Thanks & Regards
Angel AppTech
No comments:
Post a Comment
Note: only a member of this blog may post a comment.