zoukankan      html  css  js  c++  java
  • MFMailComposeViewController发送邮件的实例

     
     
     iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.  
    项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。
    #import <MessageUI/MessageUI.h>  
     
      sendMailViewController.m文件的实现:  
    1. - (void)viewDidLoad  
    2. {  
    3.     UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];  
    4.     button.frame = CGRectMake(0, 40, 320, 50);  
    5.     [button setTitle: @"Mail" forState: UIControlStateNormal];  
    6.     [button addTarget: self action: @selector(sendEMail) forControlEvents: UIControlEventTouchUpInside];  
    7.     [self.view addSubview: button];  
    8. }  
    9.   
    10. - (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg   
    11. {  
    12.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_   
    13.                                                     message:msg   
    14.                                                    delegate:nil   
    15.                                           cancelButtonTitle:@"确定"   
    16.                                           otherButtonTitles:nil];  
    17.     [alert show];  
    18.     [alert release];  
    19. }   
    20.   
    21. //点击按钮后,触发这个方法  
    22. -(void)sendEMail   
    23. {  
    24.     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));  
    25.       
    26.     if (mailClass != nil)  
    27.     {  
    28.         if ([mailClass canSendMail])  
    29.         {  
    30.             [self displayComposerSheet];  
    31.         }   
    32.         else   
    33.         {  
    34.             [self launchMailAppOnDevice];  
    35.         }  
    36.     }   
    37.     else   
    38.     {  
    39.         [self launchMailAppOnDevice];  
    40.     }      
    41. }  
    42. //可以发送邮件的话  
    43. -(void)displayComposerSheet   
    44. {  
    45.     MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];  
    46.       
    47.     mailPicker.mailComposeDelegate = self;  
    48.       
    49.     //设置主题  
    50.     [mailPicker setSubject: @"eMail主题"];  
    51.       
    52.     // 添加发送者  
    53.     NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];  
    54.     //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];  
    55.     //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];  
    56.     [mailPicker setToRecipients: toRecipients];  
    57.     //[picker setCcRecipients:ccRecipients];      
    58.     //[picker setBccRecipients:bccRecipients];  
    59.       
    60.     // 添加图片  
    61.     UIImage *addPic = [UIImage imageNamed: @"123.jpg"];  
    62.     NSData *imageData = UIImagePNGRepresentation(addPic);            // png  
    63.     // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg  
    64.     [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];  
    65.       
    66.     NSString *emailBody = @"eMail 正文";  
    67.     [mailPicker setMessageBody:emailBody isHTML:YES];  
    68.       
    69.     [self presentModalViewController: mailPicker animated:YES];  
    70.     [mailPicker release];  
    71. }  
    72. -(void)launchMailAppOnDevice  
    73. {  
    74.     NSString *recipients = @"mailto:first@example.com&subject=my email!";  
    75.     //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";  
    76.     NSString *body = @"&body=email body!";  
    77.       
    78.     NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];  
    79.     email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];  
    80.       
    81.     [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];  
    82. }  
    83. - (void)mailComposeController:(MFMailComposeViewController *)controller   
    84.           didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
    85. {  
    86.     NSString *msg;  
    87.       
    88.     switch (result)   
    89.     {  
    90.         case MFMailComposeResultCancelled:  
    91.             msg = @"邮件发送取消";  
    92.             break;  
    93.         case MFMailComposeResultSaved:  
    94.             msg = @"邮件保存成功";  
    95.             [self alertWithTitle:nil msg:msg];  
    96.             break;  
    97.         case MFMailComposeResultSent:  
    98.             msg = @"邮件发送成功";  
    99.             [self alertWithTitle:nil msg:msg];  
    100.             break;  
    101.         case MFMailComposeResultFailed:  
    102.             msg = @"邮件发送失败";  
    103.             [self alertWithTitle:nil msg:msg];  
    104.             break;  
    105.         default:  
    106.             break;  
    107.     }  
    108.       
    109.     [self dismissModalViewControllerAnimated:YES];  
  • 相关阅读:
    ToastCustomUtil【简单的Toast封装类】【自定义Toast的显示风格】
    ToastMiui【仿MIUI的带有动画的Toast】
    ToastCustom【自定义显示风格的Toast】
    用现实生活的例子解释非对称加密
    对称加密、非对称加密、数字签名、数字证书、SSL是什么
    公钥和私钥:确认过眼神 是对的人
    如何拿到美团offer的
    http无状态和鉴权解决四种方案
    java网络编程-面试题
    Java网络编程面试总结
  • 原文地址:https://www.cnblogs.com/daguo/p/2621963.html
Copyright © 2011-2022 走看看