SKPSMTPMessage实现邮件发送
2016-03-05 14:21
453 查看
iOS下发邮件目前有两种方式,利用openURL打开iOS email app和利用MFMailComposeViewController在app内弹出email界面实现邮件发送。这两种方式搜索一下都有很多介绍,具体就不细说了。下面介绍第三种方式,利用开源库SKPSMTPMessage实现邮件发送。其实这种方式也有不少文章介绍了,只是看了一些文章,写得都差不多,都是贴demo里面的代码,没有我需要的发送图片和视频附件的功能。研究和查阅了一些资料,将代码综合一下,粘贴出来方便自己和有需要的人查阅。
SKPSMTPMessage开源库下载地址:
https://github.com/jetseven/skpsmtpmessage
发送邮件,包含附件代码如下:
[cpp] view
plain copy
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。
SKPSMTPMessage开源库下载地址:
https://github.com/jetseven/skpsmtpmessage
发送邮件,包含附件代码如下:
[cpp] view
plain copy
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。
相关文章推荐
- RPM包管理-rpm命令管理
- 关于人人都是产品经理
- Ubuntu14.04 软件源
- 产品经理必备软件——Axure使用详解(3)
- [SPM_hw1]记一次项目经历
- PM 与 LM 如何分钱问题
- 安装npm
- PM HIS
- DeepMind创始人自述:我们的算法可以横扫一切棋类博弈
- 【敏捷方法落地之旅】-PMI_ACP敏捷认证交流会
- 初步了解RPM ,DPKG ,YUM ,APT-GET
- 包管理工具之npm 和bower
- 想做产品经理了
- srebmuNnoisreVerapmoC.165
- rpm方式安装部署gitlab
- npm配置镜像、设置代理
- Chipmunk物理引擎
- POPPM产品经理培训课程 -- 独家开发
- ubuntu server 14.04 编译安装xen4.4.2配置vtpm(二)——Dom0内核编译
- cannot find development files install either libnl1 or libnl3