IOS發送Email的兩種方法

來源:互聯網
上載者:User

標籤:http   color   io   os   ar   使用   for   sp   檔案   

IOS系統架構提供的兩種發送Email的方法:openURL 和 MFMailComposeViewController。藉助這兩個方法,我們可以輕鬆的在應用裡加入如使用者反饋這類需要發送郵件的功能。

 

1.openURL使用openURL調用系統郵箱用戶端是我們在IOS3.0以下實現發郵件功能的主要手段。我們可以通過設定url裡的相關參數來指定郵件的內容,不過其缺點很明顯,這樣的過程會導致程式暫時退出。下面是使用openURL來發郵件的一個小例子:
  1. #pragma mark - 使用系統郵件用戶端發送郵件   
  2. -(void)launchMailApp   
  3. {     
  4.     NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];   
  5.     //添加收件者   
  6.     NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"];   
  7.     [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];   
  8.     //添加抄送   
  9.     NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];     
  10.     [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];   
  11.     //添加密送   
  12.     NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];     
  13.     [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];   
  14.     //添加主題   
  15.     [mailUrl appendString:@"&subject=my email"];   
  16.     //添加郵件內容   
  17.     [mailUrl appendString:@"&body=<b>email</b> body!"];   
  18.     NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];     
  19.     [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];     
  20. }  

 

2.MFMailComposeViewControllerMFMailComposeViewController是在IOS3.0新增的一個介面,它在MessageUI.framework中。通過調用MFMailComposeViewController,可以把郵件發送視窗整合到我們的應用裡,發送郵件就不需要退出程式了。MFMailComposeViewController的使用方法:
  • 1.項目中引入MessageUI.framework;
  • 2.在使用的檔案中匯入MFMailComposeViewController.h標頭檔;
  • 3.實現MFMailComposeViewControllerDelegate,處理郵件發送事件;
  • 4.調出郵件發送視窗前先使用MFMailComposeViewController裡的“+ (BOOL)canSendMail”方法檢查使用者是否設定了郵件賬戶;
  • 5.初始化MFMailComposeViewController,構造郵件體

 

  1. //   
  2. //  ViewController.h   
  3. //  MailDemo   
  4. //   
  5. //  Created by LUOYL on 12-4-4.   
  6. //  Copyright (c) 2012年 http://luoyl.info. All rights reserved.   
  7. //   
  8.   
  9. #import <UIKit/UIKit.h>   
  10. #import <MessageUI/MFMailComposeViewController.h>   
  11.   
  12. @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>   
  13.   
  14. @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.     //設定主題     
  24.     [mailPicker setSubject: @"eMail主題"];     
  25.     //添加收件者   
  26.     NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"];   
  27.     [mailPicker setToRecipients: toRecipients];     
  28.     //添加抄送   
  29.     NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];     
  30.     [mailPicker setCcRecipients:ccRecipients];         
  31.     //添加密送   
  32.     NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];     
  33.     [mailPicker setBccRecipients:bccRecipients];     
  34.        
  35.     // 添加一張圖片     
  36.     UIImage *addPic = [UIImage imageNamed: @"[email protected]"];     
  37.     NSData *imageData = UIImagePNGRepresentation(addPic);            // png        
  38.     //關於mimeType:http://www.iana.org/assignments/media-types/index.html   
  39.     [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];     
  40.     
  41.     //添加一個pdf附件   
  42.     NSString *file = [self fullBundlePathFromRelativePath:@"高品質C++編程指南.pdf"];   
  43.     NSData *pdf = [NSData dataWithContentsOfFile:file];   
  44.     [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高品質C++編程指南.pdf"];     
  45.   
  46.     NSString *emailBody = @"<font color=‘red‘>eMail</font> 本文";     
  47.     [mailPicker setMessageBody:emailBody isHTML:YES];     
  48.     [self presentModalViewController: mailPicker animated:YES];     
  49.     [mailPicker release];     
  50. }   
  51.   
  52. #pragma mark - 實現 MFMailComposeViewControllerDelegate    
  53. - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
  54. {   
  55.     //關閉郵件發送視窗   
  56.     [self dismissModalViewControllerAnimated:YES];   
  57.     NSString *msg;     
  58.     switch (result) {     
  59.         case MFMailComposeResultCancelled:     
  60.             msg = @"使用者取消編輯郵件";     
  61.             break;     
  62.         case MFMailComposeResultSaved:     
  63.             msg = @"使用者成功儲存郵件";     
  64.             break;     
  65.         case MFMailComposeResultSent:     
  66.             msg = @"使用者點擊發送,將郵件放到隊列中,還沒發送";     
  67.             break;     
  68.         case MFMailComposeResultFailed:     
  69.             msg = @"使用者試圖儲存或者發送郵件失敗";     
  70.             break;     
  71.         default:     
  72.             msg = @"";   
  73.             break;     
  74.     }     
  75.     [self alertWithMessage:msg];   
  76. }   

IOS發送Email的兩種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.