GitHub - skpsmtpmessage
MIME 参考手册
我的Demo - ShowSandBoxFile_SendFileByMail_Demo
相关文章 - iOS-SKPSMTPMessage+日志缓存
一、记录使用过程
1.1、pod导入skpsmtpmessage
pod 'skpsmtpmessage'
- 不需要进行RAC修改;
-
CFNetwork.framework
被自动导入;
1.2、引入头文件
// 导入头文件
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
@interface ViewController() <SKPSMTPMessageDelegate>
@end
1.3、发送邮件
// 发送邮件 附件
- (void)sendEmail {
SKPSMTPMessage *myMessage = [[SKPSMTPMessage alloc] init];
myMessage.fromEmail = @"XXXX@163.com"; //发送邮箱
myMessage.toEmail = @"XXXX@mis.com"; //收件邮箱
myMessage.ccEmail = @"XXXX@qq.com"; //抄送 被QQ服务器退回
myMessage.relayHost = @"smtp.163.com"; //发送地址host 网易企业邮箱
myMessage.requiresAuth = YES;
myMessage.login = @"XXXX@163.com"; //发送邮箱的用户名
myMessage.pass = @"****"; //发送邮箱的密码
myMessage.wantsSecure = YES;
myMessage.subject = @"Main theme"; //邮件主题
myMessage.delegate = self;
// 文本
NSString *content = [NSString stringWithCString:"测试撒的说法的方式内容 \n /n /t fdafsasdf阿斯达发撒发多少 \\n dsasadfs多大事发的" encoding:NSUTF8StringEncoding];
NSDictionary *param =@{kSKPSMTPPartContentTypeKey :@"text/plain; charset=UTF-8",
kSKPSMTPPartMessageKey : content,
kSKPSMTPPartContentTransferEncodingKey :@"8bit"};
// 附件
NSString *txtPath = [[NSBundle mainBundle] pathForResource:@"AAAA" ofType:@"txt"];
NSData *txtData = [NSData dataWithContentsOfFile:txtPath];
if (txtData.length > 0) {
NSDictionary *txtPart = @{kSKPSMTPPartContentTypeKey:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"AAAA.txt\"",
kSKPSMTPPartContentDispositionKey:@"attachment;\r\n\tfilename=\"OSCE调试Log.txt\"",
kSKPSMTPPartMessageKey:[txtData encodeBase64ForData],
kSKPSMTPPartContentTransferEncodingKey:@"base64"};
myMessage.parts = [NSArray arrayWithObjects:param, txtPart,nil];
} else {
myMessage.parts = [NSArray arrayWithObjects:param, nil];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
[myMessage send];
[[NSRunLoop currentRunLoop] run]; //这里开启一下runloop要不然重试其他端口的操作不会进行
});
}
注意点一:myMessage.relayHost = @"smtp.163.com"; //发送地址host 网易企业邮箱
注意点二:myMessage.pass = @"****"; //发送邮箱的密码
这里的密码不是登录的密码,而是授权码。以网易163邮箱为例:
注意点三:附件不能为空,所以需要进行判断
1.4、代理方法
// 发送成功
- (void)messageSent:(SKPSMTPMessage *)message {
// 邮件发送成功
}
// 发送失败
- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error {
// 邮件发送失败
}