MFMailComposeViewController发送邮件的实例

  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: @"first@example.com"];  
  57.     //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];  
  58.     //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", 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:first@example.com&subject=my email!";  
  78.     //@"mailto:first@example.com?cc=second@example.com,third@example.com&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. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值