使用iOS开源库SKPSMTPMessage实现邮件发送
2014-01-25 13:11
375 查看
iOS下发邮件目前有两种方式,利用openURL打开iOS email app和利用MFMailComposeViewController在app内弹出email界面实现邮件发送。这两种方式搜索一下都有很多介绍,具体就不细说了。下面介绍第三种方式,利用开源库SKPSMTPMessage实现邮件发送。其实这种方式也有不少文章介绍了,只是看了一些文章,写得都差不多,都是贴demo里面的代码,没有我需要的发送图片和视频附件的功能。研究和查阅了一些资料,将代码综合一下,粘贴出来方便自己和有需要的人查阅。
SKPSMTPMessage开源库下载地址:
https://github.com/jetseven/skpsmtpmessage
发送邮件,包含附件代码如下:
[cpp]
view plaincopy
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。
转自:http://blog.csdn.net/sing_sing/article/details/8485545
SKPSMTPMessage开源库下载地址:
https://github.com/jetseven/skpsmtpmessage
发送邮件,包含附件代码如下:
[cpp]
view plaincopy
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。
转自:http://blog.csdn.net/sing_sing/article/details/8485545
相关文章推荐
- 使用iOS开源库SKPSMTPMessage实现邮件发送
- 使用iOS开源库SKPSMTPMessage实现邮件发送
- iOS使用 [SKPSMTPMessage]发送邮件
- ios 后台发送邮件之SKPSMTPMessage的使用
- oracle使用UTL_MAIL包实现存储过程邮件发送(转)
- iOS 实现发送邮件和短信
- 使用Python实现邮件发送
- 使用libcurl库实现SMTP发送邮件
- PHPMailer使用QQ邮箱实现邮件发送功能
- python使用smtplib模块通过gmail实现邮件发送的方法
- 使用Java Mail实现简单的邮件发送
- Java-使用JavaMail发送一封邮件给用户邮箱功能实现
- Oracle 11g 使用 UTL_MAIL 包实现存储过程发送邮件
- 怎么使用PHPMailer实现邮件的发送??
- 使用spring实现邮件的发送实例(含测试,源码,注释)
- 使用javax.mail实现邮件的发送
- 使用 MAPI 实现邮件发送
- C#使用smtp发送带附件的邮件实现方法
- JAVA邮件发送的简单实现,使用javamail通过smtp协议发信
- asp.net使用smtp协议实现简单的发送邮件(支持附件)