Follow the following step to send email in iOS Objective C-
Step - 1 Include the MessageUI.framework in your project . Select project go ->settings ->General->Linked Frameworks and Libraries. Tap on + and select MessageUI.framework . Following is the screen shot -
Step 2- #import <MessageUI/MessageUI.h> in your UIViewController.h file. Here is my view controller.h and viewcontroller.m file-
//
// ViewController.h
// LoginSample
//
// Created by MSClient Mac01 on 10/01/17.
// Copyright © 2017 Mindstick Software. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
- (IBAction)sendMail:(id)sender;
@end
//
// ViewController.m
// LoginSample
//
// Created by MSClient Mac01 on 10/01/17.
// Copyright © 2017 Mindstick Software. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)sendMail:(id)sender {
MFMailComposeViewController *emailController = [[MFMailComposeViewController alloc] init];
emailController.mailComposeDelegate = self;
// [emailController setSubject:@"Sample email subject "]; // set default subject
// [emailController setMessageBody:@"Hi , this is sample email body" isHTML:NO]; // set Yes if contains html
[self presentViewController:emailController animated:YES completion:NULL];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result) {
case MFMailComposeResultSent:
NSLog(@"E-mail sent successfully.");
break;
case MFMailComposeResultSaved:
NSLog(@"E-mail saved.");
break;
case MFMailComposeResultCancelled:
NSLog(@"E-mail cancelled.");
break;
case MFMailComposeResultFailed:
NSLog(@"E-mail could not be sent.");
break;
default:
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end
Following is the screen shot of MFMailComposeViewController -
Leave Comment