使用iOS开源库SKPSMTPMessage实现邮件发送
jopen
10年前
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];