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:@"
[email protected]",
nil];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"
[email protected]",
@"
[email protected]", nil];
NSArray *bccRecipients = [NSArray arrayWithObjects:@"
[email protected]",
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=@"
[email protected]";
myMessage.toEmail=@"
[email protected]";
myMessage.bccEmail=@"
[email protected]";
myMessage.relayHost=@"smtp.163.com";
myMessage.requiresAuth=YES;
if (myMessage.requiresAuth) {
myMessage.login=@"
[email protected]";
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郵箱)。郵件發送比較簡單,如有問題,可隨時與我聯系~