業務場景
最近在做一個關于bug收集的庫,其中需要收集崩潰日志信息并在后臺發送郵件給開發者。有三種方式可以實現當前的需求:1、蘋果的自帶的發送郵件方式。2、開源庫SKPSMTPMessage 3、第三方庫MailCore2。需要實現發郵件需求的同學請直接閱讀第三章節。
蘋果自帶發送郵件
本來想把發送郵件的代碼也貼上,但是實話說這個沒有什么卵用(個人覺得),界面丑到爆。而且需要手動去發送數據和個人的業務需求不符。(也可以理解為我懶??)
SKPSMTPMessage
SKPSMTPMessage是一個開源的發送郵件第三方庫,但是作者在兩年前已經停止更新。收到的郵件標題會有亂碼的。(按有的同學推薦去處理過但并沒有什卵用····)
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
//發送者
testMsg.fromEmail = @"xyhuangjia@yeah.net";
//發送給
testMsg.toEmail = @"huangj@ywsoftware.com";
//抄送聯系人列表,如:@"664742641@qq.com;1@qq.com;2@q.com;3@qq.com"
// testMsg.ccEmail = @"lanyuu@live.cn";
// //密送聯系人列表,如:@"664742641@qq.com;1@qq.com;2@q.com;3@qq.com"
// testMsg.bccEmail = @"664742641@qq.com";
//發送郵箱的發送服務器地址
testMsg.relayHost = @"smtp.yeah.net";
//需要鑒權
testMsg.requiresAuth = YES;
//發送者的登錄賬號
testMsg.login = @"xyhuangjia@yeah.net";
//發送者的登錄密碼
testMsg.pass = @"HJ19930112";
//郵件主題
testMsg.subject = [NSString stringWithCString:"來自iphone socket的測試郵件" encoding:NSUTF8StringEncoding ];
// testMsg.subject = @"測試數據";
testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS!
// Only do this for self-signed certs!
// testMsg.validateSSLChain = NO;
testMsg.delegate = self;
//主題
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
@"This is a test message.\r\n支持中文。",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
//附件
NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"video.jpg" ofType:@""];
NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath];
//附件圖片文件
NSDictionary *vcfPart = [[NSDictionary alloc ]initWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"video.jpg\"",kSKPSMTPPartContentTypeKey,
@"attachment;\r\n\tfilename=\"video.jpg\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
//附件音頻文件
NSString *wavPath = [[NSBundle mainBundle] pathForResource:@"push" ofType:@"wav"];
NSData *wavData = [NSData dataWithContentsOfFile:wavPath];
NSDictionary *wavPart = [[NSDictionary alloc ]initWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"push.wav\"",kSKPSMTPPartContentTypeKey,
@"attachment;\r\n\tfilename=\"push.wav\"",kSKPSMTPPartContentDispositionKey,[wavData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
// testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,wavPart, nil];
testMsg.parts = [NSArray arrayWithObjects:plainPart, nil];
[testMsg send];
設置代理
//MARK: SKPSMTPMessageDelegate
- (void)messageSent:(SKPSMTPMessage *)message
{
NSLog(@"send success");
// [self.view makeToast:@"發送郵件成功" duration:1 position:@"center"];
}
- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
// [self.view makeToast:[NSString stringWithFormat:@"發送郵件失敗nerror - %@",error] duration:1 position:@"center"];
NSLog(@"message - %@\nerror - %@", message, error);
}
MailCore2發送郵件
MailCore2是什么?
MailCore2是一個很強大的郵件處理第三方庫。MailCore 2提供了一個簡單而異步的Objective-C API來處理電子郵件協議IMAP,POP和SMTP。支持多種平臺。。。這個如果有興趣的話可以去看一下,傳送門
如何配置
使用stmp協議發送郵件的話需要獲取郵箱獨立密碼,我以自己的網易云郵箱作為案例來進行配置一波
第一次選擇這個
01.png
然后
02.png
大寫的尷尬。。。。。。。
還是給個官方的傳送門得了,傳送門在此
重要參數
一、用戶名密碼
登錄郵箱的發送者賬號和密碼(獨立的郵箱密碼,和登錄郵箱密碼不同,申請方式見如何配置部分)
NSString * userName = @"xyhuangjia@yeah.net";
NSString * passWord = @"不給你看";
smtpSession.username = userName;
smtpSession.password = passWord;
二、接受者的賬號
可以直接填寫郵件接收人和抄送以及密送人員名單,基本可以實現pc端郵件發送的功能
NSMutableArray *to = [[NSMutableArray alloc] init];
NSArray * recipients = @[@"2587171762@qq.com",@"huangj@ywsoftware.com"];//,@"748781314@qq.com",@"huangj@ywsoftware.com"
for(NSString *toAddress in recipients) {
MCOAddress *newAddress = [MCOAddress addressWithMailbox:toAddress];
[to addObject:newAddress];
}
[[builder header] setTo:to];
示例代碼
MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.yeah.net";
smtpSession.port = 465;
NSString * userName = @"xyhuangjia@yeah.net";
NSString * passWord = @"不給你看";
smtpSession.username = userName;
smtpSession.password = passWord;
smtpSession.connectionType = MCOConnectionTypeTLS;
MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
[[builder header] setFrom:[MCOAddress addressWithDisplayName:@"黃佳" mailbox:userName]];
/*接收人員名單*/
NSMutableArray *to = [[NSMutableArray alloc] init];
NSArray * recipients = @[@"2587171762@qq.com",@"huangj@ywsoftware.com"];//,@"748781314@qq.com",@"huangj@ywsoftware.com"
for(NSString *toAddress in recipients) {
MCOAddress *newAddress = [MCOAddress addressWithMailbox:toAddress];
[to addObject:newAddress];
}
[[builder header] setTo:to];
//抄送
// NSMutableArray *cc = [[NSMutableArray alloc] init];
// for(NSString *ccAddress in CC) {
// MCOAddress *newAddress = [MCOAddress addressWithMailbox:ccAddress];
// [cc addObject:newAddress];
// }
// [[builder header] setCc:cc];
// /*密送*/
// NSMutableArray *bcc = [[NSMutableArray alloc] init];
// for(NSString *bccAddress in BCC) {
// MCOAddress *newAddress = [MCOAddress addressWithMailbox:bccAddress];
// [bcc addObject:newAddress];
// }
// [[builder header] setBcc:bcc];
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
// app名稱
NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
//標題
NSString * subject = [NSString stringWithFormat:@"%@的Crash報告",app_Name];
[[builder header] setSubject:subject];
/*正文*/
// [builder setHTMLBody:[self createHTML:crashDictionary]];
/*
*棧信息放在附件信息里,發送給開發者。
*/
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"%@ Error sending email:%@", userName, error);
} else {
NSLog(@"%@ 成功的發送了郵件", userName);
}
}];