iOS - Sending Email

We can deliver e-mails using the E-mail program of iOS system.

Steps Involved

Step 1. Make a easy Perspective centered program.

Step 2. Choose your venture computer file, then select objectives and then add MessageUI.framework.

Step 3. Add a key in ViewController.xib and build an activity for delivering email.

Step 4. Upgrade ViewController.h as follows −
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
{
    MFMailComposeViewController *mailComposer;
}

-(IBAction)sendMail:(id)sender;

@end
Step 5. Update ViewController.m as follows −
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)sendMail:(id)sender{
    mailComposer = [[MFMailComposeViewController alloc]init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:@"Test mail"];
    [mailComposer setMessageBody:@"Testing message 
    for the test mail" isHTML:NO];
    [self presentModalViewController:mailComposer animated:YES];
}

#pragma mark - mail compose delegate
-(void)mailComposeController:(MFMailComposeViewController *)controller 
 didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
   if (result) {
        NSLog(@"Result : %d",result);
    }
    if (error) {
        NSLog(@"Error : %@",error);
    }
    [self dismissModalViewControllerAnimated:YES];

}

@end

Output

When we run the program, we'll get the following outcome −
On clicking Send Email, we will get the following output −