使用iOS開源庫SKPSMTPMessage實現郵件發送

來源:互聯網
上載者:User

iOS下發郵件目前有兩種方式,利用openURL開啟iOS email app和利用MFMailComposeViewController在app內彈出email介面實現郵件發送。這兩種方式搜尋一下都有很多介紹,具體就不細說了。下面介紹第三種方式,利用開源庫SKPSMTPMessage實現郵件發送。其實這種方式也有不少文章介紹了,只是看了一些文章,寫得都差不多,都是貼demo裡面的代碼,沒有我需要的發送圖片和視頻附件的功能。研究和查閱了一些資料,將代碼綜合一下,粘貼出來方便自己和有需要的人查閱。

SKPSMTPMessage開源庫:

https://github.com/jetseven/skpsmtpmessage

發送郵件,包含附件代碼如下:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];        SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];    testMsg.fromEmail = [defaults objectForKey:@"fromEmail"];        testMsg.toEmail = [defaults objectForKey:@"toEmail"];    testMsg.bccEmail = [defaults objectForKey:@"bccEmal"];    testMsg.relayHost = [defaults objectForKey:@"relayHost"];        testMsg.requiresAuth = [[defaults objectForKey:@"requiresAuth"] boolValue];        if (testMsg.requiresAuth) {        testMsg.login = [defaults objectForKey:@"login"];                testMsg.pass = [defaults objectForKey:@"pass"];    }        testMsg.wantsSecure = [[defaults objectForKey:@"wantsSecure"] boolValue]; // smtp.gmail.com doesn't work without TLS!        testMsg.subject = @"SMTPMessage Test Message";    //testMsg.bccEmail = @"testbcc@test.com";        // Only do this for self-signed certs!    // testMsg.validateSSLChain = NO;    testMsg.delegate = self;        //文字資訊    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,                               @"This is a tést messåge.",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];    //連絡人資訊    NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];    NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath];        NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,                             @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];    //圖片和視頻附件    //attach image    NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"jpg"];    NSData *imgData = [NSData dataWithContentsOfFile:imgPath];    NSDictionary *imagePart = [NSDictionary dictionaryWithObjectsAndKeys:@"image/jpg;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.jpg\"",kSKPSMTPPartContentTypeKey,                               @"attachment;\r\n\tfilename=\"test.jpg\"",kSKPSMTPPartContentDispositionKey,[imgData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];        //attach video    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mov"];    NSData *videoData = [NSData dataWithContentsOfFile: videoPath];    NSDictionary *videoPart = [NSDictionary dictionaryWithObjectsAndKeys:@"video/quicktime;\r\n\tx-unix-mode=0644;\r\n\tname=\"video.mov\"",kSKPSMTPPartContentTypeKey,                               @"attachment;\r\n\tfilename=\"video.mov\"",kSKPSMTPPartContentDispositionKey,[videoData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];        testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart, imagePart, videoPart, nil];        [testMsg send];

代碼是在Demo基礎上修改,經過測試,可以正常發送帶附件的email。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.