`
kingj
  • 浏览: 421444 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

ios发送邮件

 
阅读更多

IOS 程序内发邮件  

2012-03-20 10:16:54|  分类: iOS开发技术保留 |  标签:ios开发技术问题  |字号 订阅

转自:http://blog.csdn.net/bl1988530/article/details/6597230
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. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics