Saturday, May 28, 2011

Mail sending fuctionality in iPhone SDK (MFMailComposer)

Hello Friends,

Today we are going to discuss very use full topic in iPhone SDK. Many times we need to send an email from iPhone application. For this functionality apple has provided very good feature of MFMailComposer which is given by apple in iOS.  To integrate mail sending functionality from your application follow the following steps.

  • Add MessageUI.framework in your project.
  • Import following files:
  • #import <MessageUI/MessageUI.h>
    #import <MessageUI/MFMailComposeViewController.h> 
  • Add delegate in your header file like <MFMailComposeViewControllerDelegate>
  • Now write following methods in the implementation file and set send mail action on any button you want.

-(IBAction)btnEmailClick
{
 Class mailclass =(NSClassFromString(@"MFMailComposeViewController"));
 if(mailclass != nil)
 {
  if ([mailclass canSendMail])
  {
   [self displayComposerSheet];
  }
  else
  {
   [self launchMailAppOnDevice];
  }
 }
 else
 {
  [self launchMailAppOnDevice];
 }
}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
 switch (result)
 {
  case MFMailComposeResultCancelled:
  {
   break;
  }
  case MFMailComposeResultSaved:
  {
   UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Mail successfully saved!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
   [alert release];
   break;
  }
  case MFMailComposeResultSent:
  {
   UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Mail sent successfully!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
   [alert release];
   break;
  }
  case MFMailComposeResultFailed:
  {
   UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Mail sending failed!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
   [alert release];
   break;
  }
  default:
  {
   break;
  }
 }
 [self dismissModalViewControllerAnimated:YES];
}


-(void)displayComposerSheet
{
 MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
 picker.mailComposeDelegate = self;
 [picker setSubject:@""]; 
 NSString *emailBody = @"Hello World";
 [picker setMessageBody:emailBody isHTML:NO];
 
 [self presentModalViewController:picker animated:YES];
    [picker release]; 
}
-(void)launchMailAppOnDevice
{
 NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
 NSString *body =@"Hello World";
 
 NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
 email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
 
}

Everything in above code is understandable and easily customizable. So change the code according to your requirements. Yon can set default recipients,subject and mail according to your application requirements. Still if you have any doubts, please feel free to ask me.

Thanks. Enjoy.. :-)

No comments:

Post a Comment