MailCore2發送郵件(補發)

業務場景

最近在做一個關于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);
        }
    }];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,556評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 1 為何要發電子郵件? 作為職場人士,想必在工作中大家都經常收發電子郵件,一封高效得體的工作郵件勢必會讓溝通事半功...
    嘛樣閱讀 90,243評論 14 108
  • 一直走在改變自己的道路上,試圖讓自己變的更自律,可謂越挫越勇。 雖然我身邊自認為自律的人不多,但我的榜樣時間里...
    大大的世界_小小的生活閱讀 569評論 0 0
  • 1、 今天開扒唐僧的坐騎,小白龍。他是龍王敖閏的三太子。 在神話故事里,龍王總是扮演著興云布雨的角色。 但是在《西...
    c3c3540e6438閱讀 1,704評論 0 4