Friday, June 17, 2011

iPhone Email - MFMailcomposeviewcontroller

In this Article we will add Email Composer in our iPhone Application

Step 1:

You have to add framework for MPMailComposer

#import <MessageUI/MessageUI.h>

#import <MessageUI/MFMailComposeViewController.h>

Step 2:

Add below mention code in you iPhone application

-(void)sendButtonClicked

{

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

if (mailClass != nil){

// To check whether the current device is configured for sending emails or not

if ([mailClass canSendMail]){    [self displayComposerSheet];  }

else{   [self launchMailAppOnDevice];  }

}

else{  [self launchMailAppOnDevice];}

} // End sendButtonClicked

-(void)displayComposerSheet

{

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

picker.mailComposeDelegate = self;

[picker setSubject:@"Enter Subject line here!"];

// Set up recipients

NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];

NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];

NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];

[picker setToRecipients:toRecipients];

[picker setCcRecipients:ccRecipients];

[picker setBccRecipients:bccRecipients];

// To Attach an image to the email

NSString *path = [[NSBundle mainBundle] pathForResource:@"attachment" ofType:@"png"];

NSData *myData = [NSData dataWithContentsOfFile:path];

[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"attachment"];

// Add text to email body

NSString *emailBody = @"This is email body, change text as per your requirements!";

[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];

[picker release];

} // End displayComposerSheet

// To Dismisses the email composition interface when users tap on Cancel or Send. Proceeds to update the message field with the result of the operation.

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

{

message.hidden = NO;

// show alert to users about errors associated with the interface

switch (result)

{

case MFMailComposeResultCancelled:

message.text = @"Result: canceled";

break;

case MFMailComposeResultSaved:

message.text = @"Result: saved";

break;

case MFMailComposeResultSent:

message.text = @"Result: sent";

break;

case MFMailComposeResultFailed:

message.text = @"Result: failed";

break;

default:

message.text = @"Result: not sent";

break;

}

[self dismissModalViewControllerAnimated:YES];

}

// Launches the Mail application on the device.

-(void)launchMailAppOnDevice {

NSString *recipients = @"mailto:first@example.com?                   cc=second@example.com,third@example.com&subject=Enter Subject line here!";

NSString *body = @"&body=This is email body, change text as per your requirements.!";

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];

email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

}

No comments:

Post a Comment

Kamleshwar