使用SKPSMTPMessage库发送 邮件地址:https://github.com/jetseven/skpsmtpmessage
这个库比较老旧,使用MRC手动内存管理(如下),当然也可以用pod直接导入,而且省事方便
引入头文件
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
遵循代理 SKPSMTPMessageDelegate
//代码如下
-(void)sendEmail{
SKPSMTPMessage *testMsg = [[SKPSMTPMessagealloc] init];
//发送邮箱
testMsg.fromEmail =@"houcr@hi-service.cn";
//收件邮箱
testMsg.toEmail =@"houcr@hi-service.cn";
testMsg.relayHost =@"mail.hi-service.cn";
testMsg.requiresAuth =YES;
//发送邮箱
testMsg.login =@"houcr@hi-service.cn";
//发送邮箱密码
testMsg.pass =@"********";
testMsg.wantsSecure =YES;
testMsg.delegate =self;
//主题
testMsg.subject = [NSStringstringWithFormat:@"%@_%@",info.companyName,info.userNames];
//内容
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,[NSStringstringWithCString:"测试正文"encoding:NSUTF8StringEncoding], kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
NSDictionary *vcfPart = [NSDictionarydictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,[NSStringstringWithFormat:@"%@",contentStr],kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
//添加附件图片
NSData *imgData =UIImagePNGRepresentation(_selectIcon);
NSDictionary *imagePart = [NSDictionarydictionaryWithObjectsAndKeys:@"image/png;\r\n\tx-unix-mode=0644;\r\n\tname=\"backIcon.png\"",kSKPSMTPPartContentTypeKey, @"attachment;\r\n\tfilename=\"backIcon.png\"",kSKPSMTPPartContentDispositionKey,[imgDataencodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
testMsg.parts = [NSArrayarrayWithObjects:plainPart,vcfPart,imagePart,nil];
[testMsg send];
}
//代理方法
- (void)messageSent:(SKPSMTPMessage *)message
{
[AlertshowWithTitle:@"提交成功"];
[self.navigationControllerpopViewControllerAnimated:YES];
NSLog(@"%@", message);
}
- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
[AlertshowWithTitle:@"提交失败"];
NSLog(@"message - %@\nerror - %@", message, error);
}
到此为止写完了,先不用激动,后边还有坑,当发送成功收到邮件时,一脸蒙蔽了,标题乱码了,经过一系列的编码尝试都不行,这时要修改库里的编码方式,先找到SKPSMTPMessage.m这个类,869行编码格式改为:NSUTF8StringEncoding