標籤:
使用MFMailComposeViewController發送郵件
1.項目需要匯入架構:MessageUI.framework
2.使用的Controller的.h檔案中添加代理 MFMailComposeViewControllerDelegate 並且匯入標頭檔:#import <MessageUI/MessageUI.h>
3.判斷使用者是否設定了郵箱賬戶
BOOL canSend = [MFMailComposeViewController canSendMail];if (canSend) { [self sendEmailAction];}else{ //提示使用者添加郵件賬戶 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Please Add Your Mail Account:" message:@"Settings->Mail,Contacts,Calendars->Add Account" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:[kGlobal getStringValueWithKey:@"OK"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil];}
4.sendEmailAction方法代碼
- (void)sendEmailAction{ // 郵件伺服器 MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init]; // 設定郵件代理 [mailCompose setMailComposeDelegate:self]; // 設定郵件主題 [mailCompose setSubject:@"我是郵件主題"]; // 設定收件者 [mailCompose setToRecipients:@[@"[email protected]"]]; // 設定抄送人 [mailCompose setCcRecipients:@[@"[email protected]"]]; // 設定密抄送 [mailCompose setBccRecipients:@[@"[email protected]"]]; /** * 設定郵件的本文內容 */ NSString *emailContent = @"我是郵件內容"; // 是否為HTML格式 [mailCompose setMessageBody:emailContent isHTML:NO]; // 如使用HTML格式,則為以下代碼 /** NSData *imageData = UIImageJPEGRepresentation(self.cellImage, 0.5); NSString *encodedString = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn]; NSString *emailContent = [NSString stringWithFormat:@"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><head><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"></head><body><img src=\"data:image/jpeg;base64,%@\" /><p><table width=\"300px\"><tr><td colspan=\"2\" align=\"left\" style=\"color:red;font-style:italic\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td colspan=\"2\" align=\"left\" style=\"color:red;font-style:italic\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr><tr><td>%@</td><td align=\"right\">%@</td></tr></table></p></body></html>",encodedString,firstTitle,[kGlobal getStringValueWithKey:@"casePrice"],[Global getFormatStr:self.offering.case_price],[kGlobal getStringValueWithKey:@"casesneeded"],[Global getFormatStr:self.offering.cases_needed],[kGlobal getStringValueWithKey:@"productsavingcases"],[NSString stringWithFormat:@"$%@",[Global getFormatStr:self.offering.product_savings]],[kGlobal getStringValueWithKey:@"annualSaving"],[NSString stringWithFormat:@"$%@",[Global getFormatStr:self.offering.annual_savings]],secondTitle,[kGlobal getStringValueWithKey:@"pricepaidpercase"],[Global getFormatStr:self.operatorVolume.pricePerCase],[kGlobal getStringValueWithKey:@"casespurchasedannually"],[Global getFormatStr:self.competitorProduct.cases_purchase_annuaully],[kGlobal getStringValueWithKey:@"costofcompetitiveproduct"],[Global getFormatStr:self.competitorProduct.cost_of_competitive_product]];//直接把圖片轉成Base64編碼放到郵件裡面,注意Base64編碼圖片的格式(PNG,JPEG)要和壓縮的格式一樣,否則不能顯示 */
//添加附件 //添加圖片附件 UIImage *image = [UIImage imageNamed:@"image"]; NSData *imageData = UIImagePNGRepresentation(image); [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"custom.png"]; //添加PDF附件 NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]; NSData *pdf = [NSData dataWithContentsOfFile:file]; [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"test"]; // 彈出郵件發送視圖 [self presentViewController:mailCompose animated:YES completion:nil];}
5.MFMailComposeViewControllerDelegate的代理方法:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ switch (result) { case MFMailComposeResultCancelled: // 使用者取消編輯 NSLog(@"Mail send canceled..."); break; case MFMailComposeResultSaved: // 使用者儲存郵件 NSLog(@"Mail saved..."); break; case MFMailComposeResultSent: // 使用者點擊發送 NSLog(@"Mail sent..."); break; case MFMailComposeResultFailed: // 使用者嘗試儲存或發送郵件失敗 NSLog(@"Mail send errored: %@...", [error localizedDescription]); break; } // 關閉郵件發送視圖 [self dismissViewControllerAnimated:YES completion:nil];}
需要注意的事項:
1.郵件的發送必須真機測試,ipad或iPhone測試都行,否則會崩潰
2.添加的附件在郵件編輯的過程中會直接顯示在本文內容後面,發送之後需要下載
3.發送非HTML內容時,本文內容是一個NSString類型的字串;如果發送HTML類型的資料,需要注意盡量寫標準的HTML格式,不然郵件無法解析,我上面寫的發送的HTML格式的內容中,實際上混合了一張圖片和一個表格,圖片採用的是Base64的編碼格式,瀏覽器可以直接解析Base64編碼,但是注意郵件不是瀏覽器,郵件對HTML的格式要求更加嚴格。我就因為載入JPEG格式的圖片時,在HTML中使用了PNG格式的Base64的解碼方式,結果在瀏覽器中可以正常顯示,郵件中無法顯示。而且通過Base64嵌入到網頁中的圖片有大小限制,盡量嵌入小圖片。
Data URI scheme支援的類型有:
data:,文本資料
data:text/plain,文本資料
data:text/html,HTML代碼
data:text/html;base64,base64編碼的HTML代碼
data:text/css,CSS代碼
data:text/css;base64,base64編碼的CSS代碼
data:text/javascript,Javascript代碼
data:text/javascript;base64,base64編碼的Javascript代碼
data:image/gif;base64,base64編碼的gif圖片資料
data:image/png;base64,base64編碼的png圖片資料
data:image/jpeg;base64,base64編碼的jpeg圖片資料
data:image/x-icon;base64,base64編碼的icon圖片資料
iOS調用系統功能發郵件