MFMailComposeViewController发送邮件的实例

[cpp]  view plain copy
  1. iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.  
  2. 项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。#import <MessageUI/MessageUI.h>  
  3. sendMailViewController.m文件的实现:  
  4. - (void)viewDidLoad  
  5. {  
  6.     UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];  
  7.     button.frame = CGRectMake(0, 40, 320, 50);  
  8.     [button setTitle: @"Mail" forState: UIControlStateNormal];  
  9.     [button addTarget: self action: @selector(sendEMail) forControlEvents: UIControlEventTouchUpInside];  
  10.     [self.view addSubview: button];  
  11. }  
  12.   
  13. - (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg   
  14. {  
  15.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_   
  16.                                                     message:msg   
  17.                                                    delegate:nil   
  18.                                           cancelButtonTitle:@"确定"   
  19.                                           otherButtonTitles:nil];  
  20.     [alert show];  
  21.     [alert release];  
  22. }   
  23.   
  24. //点击按钮后,触发这个方法  
  25. -(void)sendEMail   
  26. {  
  27.     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));  
  28.       
  29.     if (mailClass != nil)  
  30.     {  
  31.         if ([mailClass canSendMail])  
  32.         {  
  33.             [self displayComposerSheet];  
  34.         }   
  35.         else   
  36.         {  
  37.             [self launchMailAppOnDevice];  
  38.         }  
  39.     }   
  40.     else   
  41.     {  
  42.         [self launchMailAppOnDevice];  
  43.     }      
  44. }  
  45. //可以发送邮件的话  
  46. -(void)displayComposerSheet   
  47. {  
  48.     MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];  
  49.       
  50.     mailPicker.mailComposeDelegate = self;  
  51.       
  52.     //设置主题  
  53.     [mailPicker setSubject: @"eMail主题"];  
  54.       
  55.     // 添加发送者  
  56.     NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"];  
  57.     //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];  
  58.     //NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]", nil];  
  59.     [mailPicker setToRecipients: toRecipients];  
  60.     //[picker setCcRecipients:ccRecipients];      
  61.     //[picker setBccRecipients:bccRecipients];  
  62.       
  63.     // 添加图片  
  64.     UIImage *addPic = [UIImage imageNamed: @"123.jpg"];  
  65.     NSData *imageData = UIImagePNGRepresentation(addPic);            // png  
  66.     // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg  
  67.     [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];  
  68.       
  69.     NSString *emailBody = @"eMail 正文";  
  70.     [mailPicker setMessageBody:emailBody isHTML:YES];  
  71.       
  72.     [self presentModalViewController: mailPicker animated:YES];  
  73.     [mailPicker release];  
  74. }  
  75. -(void)launchMailAppOnDevice  
  76. {  
  77.     NSString *recipients = @"mailto:[email protected]&subject=my email!";  
  78.     //@"mailto:[email protected][email protected],[email protected]&subject=my email!";  
  79.     NSString *body = @"&body=email body!";  
  80.       
  81.     NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];  
  82.     email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];  
  83.       
  84.     [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];  
  85. }  
  86. - (void)mailComposeController:(MFMailComposeViewController *)controller   
  87.           didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
  88. {  
  89.     NSString *msg;  
  90.       
  91.     switch (result)   
  92.     {  
  93.         case MFMailComposeResultCancelled:  
  94.             msg = @"邮件发送取消";  
  95.             break;  
  96.         case MFMailComposeResultSaved:  
  97.             msg = @"邮件保存成功";  
  98.             [self alertWithTitle:nil msg:msg];  
  99.             break;  
  100.         case MFMailComposeResultSent:  
  101.             msg = @"邮件发送成功";  
  102.             [self alertWithTitle:nil msg:msg];  
  103.             break;  
  104.         case MFMailComposeResultFailed:  
  105.             msg = @"邮件发送失败";  
  106.             [self alertWithTitle:nil msg:msg];  
  107.             break;  
  108.         default:  
  109.             break;  
  110.     }  
  111.       
  112.     [self dismissModalViewControllerAnimated:YES];  
  113. }  

你可能感兴趣的:(email,button)