天天看點

IOS發送Email的兩種方法-備

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.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. //  Created by LUOYL on 12-4-4.   
  5. //  Copyright (c) 2012年 http://luoyl.info. All rights reserved.   
  6. #import <UIKit/UIKit.h>   
  7. #import <MessageUI/MFMailComposeViewController.h>   
  8. @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>   
  9. @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.     [self displayMailPicker];   
  13. }   
  14. //調出郵件發送視窗   
  15. - (void)displayMailPicker   
  16.     MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];     
  17.     mailPicker.mailComposeDelegate = self;     
  18.     //設定主題     
  19.     [mailPicker setSubject: @"eMail主題"];     
  20.     [mailPicker setToRecipients: toRecipients];     
  21.     [mailPicker setCcRecipients:ccRecipients];         
  22.     [mailPicker setBccRecipients:bccRecipients];     
  23.     // 添加一張圖檔     
  24.     UIImage *addPic = [UIImage imageNamed: @"[email protected]"];     
  25.     NSData *imageData = UIImagePNGRepresentation(addPic);            // png        
  26.     //關于mimeType:http://www.iana.org/assignments/media-types/index.html   
  27.     [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];     
  28.     //添加一個pdf附件   
  29.     NSString *file = [self fullBundlePathFromRelativePath:@"高品質C++程式設計指南.pdf"];   
  30.     NSData *pdf = [NSData dataWithContentsOfFile:file];   
  31.     [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高品質C++程式設計指南.pdf"];     
  32.     NSString *emailBody = @"<font color='red'>eMail</font> 正文";     
  33.     [mailPicker setMessageBody:emailBody isHTML:YES];     
  34.     [self presentModalViewController: mailPicker animated:YES];     
  35.     [mailPicker release];     
  36. #pragma mark - 實作 MFMailComposeViewControllerDelegate    
  37. - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
  38.     //關閉郵件發送視窗   
  39.     [self dismissModalViewControllerAnimated:YES];   
  40.     NSString *msg;     
  41.     switch (result) {     
  42.         case MFMailComposeResultCancelled:     
  43.             msg = @"使用者取消編輯郵件";     
  44.             break;     
  45.         case MFMailComposeResultSaved:     
  46.             msg = @"使用者成功儲存郵件";     
  47.         case MFMailComposeResultSent:     
  48.             msg = @"使用者點選發送,将郵件放到隊列中,還沒發送";     
  49.         case MFMailComposeResultFailed:     
  50.             msg = @"使用者試圖儲存或者發送郵件失敗";     
  51.         default:     
  52.             msg = @"";   
  53.     }     
  54.     [self alertWithMessage:msg];