iOS開發-郵件發送
Web開發的時候郵箱註冊登入是必不可少的,手機號可以更換,不過相對而言,郵箱只是用於比較重要的時候用到,比如找工作的時候必填的郵箱,註冊網站會員的郵箱驗證。現在的手機和Web的其實操作是一樣的,大多數情況還是郵件的發送,iOS上有兩種郵件發送的方式,一種是使用內建的MFMailComposeViewController發送郵件,第二種就是通過第三方類庫SKPSMTPMessage發送郵件 MFMailComposeViewController發送 iOS3.0之後你能通過內建的MFMailComposeViewController構成一個郵件發送介面,不過模擬器上不支援,而且預設的介面不太符合中國人郵件發送的習慣,但是iOS既然有了,還是簡單看下效果: 首先匯入MessageUI(#import <MessageUI/MessageUI.h>),實現MFMailComposeViewControllerDelegate協議; 郵件發送方法: -(void)displayComposerSheet{ MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; //設定主題 [picker setSubject:@"部落格園-FlyElephant"]; //設定收件者 NSArray *toRecipients = [NSArray arrayWithObjects:@"FlyElephant@163.com", nil]; NSArray *ccRecipients = [NSArray arrayWithObjects:@"10000@qq.com", @"10000@sina.com", nil]; NSArray *bccRecipients = [NSArray arrayWithObjects:@"keso@sina.com", nil]; [picker setToRecipients:toRecipients]; [picker setCcRecipients:ccRecipients]; [picker setBccRecipients:bccRecipients]; //設定附件為圖片 NSString *path = [[NSBundle mainBundle] pathForResource:@"man" ofType:@"jpg"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"man"]; // 設定郵件發送內容 NSString *emailBody = @"IOS中的個人部落格地址:http://www.cnblogs.com/xiaofeixiang"; [picker setMessageBody:emailBody isHTML:NO]; //郵件發送的模態視窗 [self presentModalViewController:picker animated:YES];} 郵件發送完成調用的方法: -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ switch (result) { case MFMailComposeResultCancelled: //取消 NSLog(@"MFMailComposeResultCancelled-取消"); break; case MFMailComposeResultSaved: // 儲存 NSLog(@"MFMailComposeResultSaved-儲存郵件"); break; case MFMailComposeResultSent: // 發送 NSLog(@"MFMailComposeResultSent-發送郵件"); break; case MFMailComposeResultFailed: // 嘗試儲存或發送郵件失敗 NSLog(@"MFMailComposeResultFailed: %@...",[error localizedDescription]); break; } // 關閉郵件發送視圖 [self dismissViewControllerAnimated:YES completion:nil];} SKPSMTPMessage發送 這個第三方類庫出現的比較早,引用的也比較多,把常用的郵件發送都封裝到了起來,直接調用,是一個很簡單的事情,下載之後會看到一個Demo和一個SMTPLibrary,直接將SMTPLibray拷貝到項目中,引用即可: ①需要發送的控制器上匯入SKPSMTPMessage和NSData+Base64Additions標頭檔 #import "SKPSMTPMessage.h" #import "NSData+Base64Additions.h" ②實現SKPSMTPMessageDelegate協議; ③具體發送方法: SKPSMTPMessage *myMessage = [[SKPSMTPMessage alloc] init]; myMessage.fromEmail=@"keso@163.com"; myMessage.toEmail=@"FlyElephant@qq.com"; myMessage.bccEmail=@"Goodman@qq.com"; myMessage.relayHost=@"smtp.163.com"; myMessage.requiresAuth=YES; if (myMessage.requiresAuth) { myMessage.login=@"keso@163.com"; myMessage.pass=@"keso"; } myMessage.wantsSecure =YES; //為gmail郵箱設定 smtp.gmail.com myMessage.subject = @"部落格園-FlyElephant"; myMessage.delegate = self; //設定郵件內容 NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey, @"做一個簡簡單單,快快樂樂的程式員~keso",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; myMessage.parts = [NSArray arrayWithObjects:plainPart,nil]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [myMessage send]; });④設定發送之後的處理: - (void)messageSent:(SKPSMTPMessage *)message{ NSLog(@"恭喜,郵件發送成功");} - (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{ NSLog(@"不好意思,郵件發送失敗"); } 上面的本人通過163郵箱發送到qq郵箱,其他郵箱之間的發送沒有嘗試過,以上已經實現了簡單的郵件發送,具體郵箱之間的設定,可以參考之前郵件發送文章: C#郵件發送(最坑爹的郵箱-QQ郵箱)。郵件發送比較簡單,如有問題,可隨時與我聯絡~