iOS_开发_Send_Mail

来源:互联网 发布:js去重最优算法 编辑:程序博客网 时间:2024/05/16 18:24
  1. MFMailComposeViewController

MFMailComposeViewController是在IOS3.0新增的一个接口,它在MessageUI.framework中。通过调用MFMailComposeViewController,可以把邮件发送窗口集成到我们的应用里,发送邮件就不需要退出程序了。MFMailComposeViewController的使用方法:

  • 1.项目中引入MessageUI.framework;
  • 2.在使用的文件中导入MFMailComposeViewController.h头文件;
  • 3.实现MFMailComposeViewControllerDelegate,处理邮件发送事件;
  • 4.调出邮件发送窗口前先使用MFMailComposeViewController里的“+ (BOOL)canSendMail”方法检查用户是否设置了邮件账户;
  • 5.初始化MFMailComposeViewController,构造邮件体
  1. #import    
  2. #import    
  3. @interface ViewController UIViewController    
  4. @end   
  1. #pragma mark 在应用内发送邮件   
  2. //激活邮件功能   
  3. (void)sendMailInApp   
  4. {   
  5.     Class mailClass (NSClassFromString(@"MFMailComposeViewController"));    
  6.     if (!mailClass) {   
  7.         [self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"];   
  8.         return;   
  9.     }   
  10.     if (![mailClass canSendMail]) {   
  11.         [self alertWithMessage:@"用户没有设置邮件账户"];   
  12.         return;   
  13.     }   
  14.     [self displayMailPicker];   
  15. }   
  16.   
  17. //调出邮件发送窗口   
  18. (void)displayMailPicker   
  19. {   
  20.     MFMailComposeViewController *mailPicker [[MFMailComposeViewController alloc] init];     
  21.     mailPicker.mailComposeDelegate self;       
  22.     //设置主题     
  23.     [mailPicker setSubject: @"eMail主题"];     
  24.     //添加收件人   
  25.     NSArray *toRecipients [NSArray arrayWithObject: @"first@example.com"];   
  26.     [mailPicker setToRecipients: toRecipients];     
  27.     //添加抄送   
  28.     NSArray *ccRecipients [NSArray arrayWithObjects:@"second@example.com"@"third@example.com"nil];     
  29.     [mailPicker setCcRecipients:ccRecipients];         
  30.     //添加密送   
  31.     NSArray *bccRecipients [NSArray arrayWithObjects:@"fourth@example.com"nil];     
  32.     [mailPicker setBccRecipients:bccRecipients];     
  33.        
  34.     // 添加一张图片     
  35.     UIImage *addPic [UIImage imageNamed: @"Icon@2x.png"];     
  36.     NSData *imageData UIImagePNGRepresentation(addPic);            // png        
  37.     //关于mimeType:http://www.iana.org/assignments/media-types/index.html   
  38.     [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];     
  39.     
  40.     //添加一个pdf附件   
  41.     NSString *file [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];   
  42.     NSData *pdf [NSData dataWithContentsOfFile:file];   
  43.     [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];     
  44.   //添加一个视频
  45.     NSString *path=[NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"20121219.avi"];

        NSData *video = [NSData dataWithContentsOfFile:path];

        [mailPicker addAttachmentData:video mimeType: @"" fileName:@"20121219.avi"];

  46.     NSString *emailBody @"eMail 正文"    
  47.     [mailPicker setMessageBody:emailBody isHTML:YES];     
  48.     [self presentModalViewController: mailPicker animated:YES];     
  49.     [mailPicker release];     
  50. }   
  51. #pragma mark 实现 MFMailComposeViewControllerDelegate    
  52. (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
  53. {    //关闭邮件发送窗口   
  54.     [self dismissModalViewControllerAnimated:YES];   
  55.     NSString *msg;     
  56.     switch (result)     
  57.         case MFMailComposeResultCancelled:     
  58.             msg @"用户取消编辑邮件"    
  59.             break    
  60.         case MFMailComposeResultSaved    
  61.             msg @"用户成功保存邮件"    
  62.             break    
  63.         case MFMailComposeResultSent:     
  64.             msg @"用户点击发送,将邮件放到队列中,还没发送"    
  65.             break    
  66.         case MFMailComposeResultFailed:     
  67.             msg @"用户试图保存或者发送邮件失败"    
  68.             break    
  69.         default    
  70.             msg @"";   
  71.             break    
  72.         
  73.     [self alertWithMessage:msg];   
  74. }   
0 0
原创粉丝点击