業(yè)務(wù)場(chǎng)景
最近在做一個(gè)關(guān)于bug收集的庫(kù),其中需要收集崩潰日志信息并在后臺(tái)發(fā)送郵件給開(kāi)發(fā)者。有三種方式可以實(shí)現(xiàn)當(dāng)前的需求:1、蘋(píng)果的自帶的發(fā)送郵件方式。2、開(kāi)源庫(kù)SKPSMTPMessage 3、第三方庫(kù)MailCore2。需要實(shí)現(xiàn)發(fā)郵件需求的同學(xué)請(qǐng)直接閱讀第三章節(jié)。
蘋(píng)果自帶發(fā)送郵件
本來(lái)想把發(fā)送郵件的代碼也貼上,但是實(shí)話說(shuō)這個(gè)沒(méi)有什么卵用(個(gè)人覺(jué)得),界面丑到爆。而且需要手動(dòng)去發(fā)送數(shù)據(jù)和個(gè)人的業(yè)務(wù)需求不符。(也可以理解為我懶??)
SKPSMTPMessage
SKPSMTPMessage是一個(gè)開(kāi)源的發(fā)送郵件第三方庫(kù),但是作者在兩年前已經(jīng)停止更新。收到的郵件標(biāo)題會(huì)有亂碼的。(按有的同學(xué)推薦去處理過(guò)但并沒(méi)有什卵用····)
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
//發(fā)送者
testMsg.fromEmail = @"xyhuangjia@yeah.net";
//發(fā)送給
testMsg.toEmail = @"huangj@ywsoftware.com";
//抄送聯(lián)系人列表,如:@"664742641@qq.com;1@qq.com;2@q.com;3@qq.com"
// testMsg.ccEmail = @"lanyuu@live.cn";
// //密送聯(lián)系人列表,如:@"664742641@qq.com;1@qq.com;2@q.com;3@qq.com"
// testMsg.bccEmail = @"664742641@qq.com";
//發(fā)送郵箱的發(fā)送服務(wù)器地址
testMsg.relayHost = @"smtp.yeah.net";
//需要鑒權(quán)
testMsg.requiresAuth = YES;
//發(fā)送者的登錄賬號(hào)
testMsg.login = @"xyhuangjia@yeah.net";
//發(fā)送者的登錄密碼
testMsg.pass = @"HJ19930112";
//郵件主題
testMsg.subject = [NSString stringWithCString:"來(lái)自iphone socket的測(cè)試郵件" encoding:NSUTF8StringEncoding ];
// testMsg.subject = @"測(cè)試數(shù)據(jù)";
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];
設(shè)置代理
//MARK: SKPSMTPMessageDelegate
- (void)messageSent:(SKPSMTPMessage *)message
{
NSLog(@"send success");
// [self.view makeToast:@"發(fā)送郵件成功" duration:1 position:@"center"];
}
- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
// [self.view makeToast:[NSString stringWithFormat:@"發(fā)送郵件失敗nerror - %@",error] duration:1 position:@"center"];
NSLog(@"message - %@\nerror - %@", message, error);
}
MailCore2發(fā)送郵件
MailCore2是什么?
MailCore2是一個(gè)很強(qiáng)大的郵件處理第三方庫(kù)。MailCore 2提供了一個(gè)簡(jiǎn)單而異步的Objective-C API來(lái)處理電子郵件協(xié)議IMAP,POP和SMTP。支持多種平臺(tái)。。。這個(gè)如果有興趣的話可以去看一下,傳送門
如何配置
使用stmp協(xié)議發(fā)送郵件的話需要獲取郵箱獨(dú)立密碼,我以自己的網(wǎng)易云郵箱作為案例來(lái)進(jìn)行配置一波
第一次選擇這個(gè)

然后

大寫(xiě)的尷尬。。。。。。。
還是給個(gè)官方的傳送門得了,傳送門在此
重要參數(shù)
一、用戶名密碼
登錄郵箱的發(fā)送者賬號(hào)和密碼(獨(dú)立的郵箱密碼,和登錄郵箱密碼不同,申請(qǐng)方式見(jiàn)如何配置部分)
NSString * userName = @"xyhuangjia@yeah.net";
NSString * passWord = @"不給你看";
smtpSession.username = userName;
smtpSession.password = passWord;
二、接受者的賬號(hào)
可以直接填寫(xiě)郵件接收人和抄送以及密送人員名單,基本可以實(shí)現(xiàn)pc端郵件發(fā)送的功能
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"];
//標(biāo)題
NSString * subject = [NSString stringWithFormat:@"%@的Crash報(bào)告",app_Name];
[[builder header] setSubject:subject];
/*正文*/
// [builder setHTMLBody:[self createHTML:crashDictionary]];
/*
*棧信息放在附件信息里,發(fā)送給開(kāi)發(fā)者。
*/
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"%@ Error sending email:%@", userName, error);
} else {
NSLog(@"%@ 成功的發(fā)送了郵件", userName);
}
}];