使用iOS开源库SKPSMTPMessage实现邮件发送

iOS下发邮件目前有两种方式,利用openURL打开iOS email app和利用MFMailComposeViewController在app内弹出email界面实现邮件发送。这两种方式搜索一下都有很多介绍,具体就不细说了。下面介绍第三种方式,利用开源库SKPSMTPMessage实现邮件发送。其实这种方式也有不少文章介绍了,只是看了一些文章,写得都差不多,都是贴demo里面的代码,没有我需要的发送图片和视频附件的功能。研究和查阅了一些资料,将代码综合一下,粘贴出来方便自己和有需要的人查阅。

SKPSMTPMessage开源库下载地址:

https://github.com/jetseven/skpsmtpmessage

发送邮件,包含附件代码如下:


  1. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];  
  2.       
  3.     SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];  
  4.     testMsg.fromEmail = [defaults objectForKey:@"fromEmail"];  
  5.       
  6.     testMsg.toEmail = [defaults objectForKey:@"toEmail"];  
  7.     testMsg.bccEmail = [defaults objectForKey:@"bccEmal"];  
  8.     testMsg.relayHost = [defaults objectForKey:@"relayHost"];  
  9.       
  10.     testMsg.requiresAuth = [[defaults objectForKey:@"requiresAuth"] boolValue];  
  11.       
  12.     if (testMsg.requiresAuth) {  
  13.         testMsg.login = [defaults objectForKey:@"login"];  
  14.           
  15.         testMsg.pass = [defaults objectForKey:@"pass"];  
  16.   
  17.     }  
  18.       
  19.     testMsg.wantsSecure = [[defaults objectForKey:@"wantsSecure"] boolValue]; // smtp.gmail.com doesn't work without TLS!  
  20.   
  21.       
  22.     testMsg.subject = @"SMTPMessage Test Message";  
  23.     //testMsg.bccEmail = @"testbcc@test.com";  
  24.       
  25.     // Only do this for self-signed certs!  
  26.     // testMsg.validateSSLChain = NO;  
  27.     testMsg.delegate = self;  
  28.       
  29.     //文字信息  
  30.     NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,  
  31.                                @"This is a tést messåge.",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];  
  32.     //联系人信息  
  33.     NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];  
  34.     NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath];  
  35.       
  36.     NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,  
  37.                              @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];  
  38.     //图片和视频附件  
  39.     //attach image  
  40.     NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"jpg"];  
  41.     NSData *imgData = [NSData dataWithContentsOfFile:imgPath];  
  42.     NSDictionary *imagePart = [NSDictionary dictionaryWithObjectsAndKeys:@"image/jpg;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.jpg\"",kSKPSMTPPartContentTypeKey,  
  43.                                @"attachment;\r\n\tfilename=\"test.jpg\"",kSKPSMTPPartContentDispositionKey,[imgData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];  
  44.       
  45.     //attach video  
  46.     NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mov"];  
  47.     NSData *videoData = [NSData dataWithContentsOfFile: videoPath];  
  48.     NSDictionary *videoPart = [NSDictionary dictionaryWithObjectsAndKeys:@"video/quicktime;\r\n\tx-unix-mode=0644;\r\n\tname=\"video.mov\"",kSKPSMTPPartContentTypeKey,  
  49.                                @"attachment;\r\n\tfilename=\"video.mov\"",kSKPSMTPPartContentDispositionKey,[videoData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];  
  50.       
  51.     testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart, imagePart, videoPart, nil];  
  52.       
  53.     [testMsg send];  

代码是在Demo基础上修改,经过测试,可以正常发送带附件的email。


转自:http://blog.csdn.net/sing_sing/article/details/8485545

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值