1. 程式人生 > >Email 的兩種方法

Email 的兩種方法

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 subject"];   
  16.     //新增郵件內容   
  17.     [mailUrl appendString:@"&body=email body"];   
  18.     NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];     
  19.     [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];     
  20. }  

 

2.MFMailComposeViewController

MFMailComposeViewController是在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. }