skpsmtpmessage 是ios第三方后台发送邮件库 https://github.com/jetseven/skpsmtpmessage.git
1、由于skpsmtpmessage是非ARC文件,所以要在 SKPSMTPMessage和 NSData+Base64Additions中添加
Your Target -> Get Info -> Build -> All Configurations -> Other Link Flags: -fno-objc-arc 标识
2、导入cfnetwork.frame框架
导入头文件#import "SKPSMTPMessage.h" #import "NSData+Base64Additions.h"
SKPSMTPMessageDelegate协议,并实现两个协议方法
3、开始发送邮件
// 发送邮件 - (IBAction)sendEmail { SKPSMTPMessage *testSend = [[SKPSMTPMessage alloc]init]; testSend.fromEmail = @"发送邮箱账号"; testSend.toEmail = @"接收邮箱账号"; testSend.relayHost = @"smtp.ym.163.com"; testSend.requiresAuth = YES; testSend.login = @"发送邮箱账号"; testSend.pass = @"发送邮箱密码"; testSend.subject = [NSString stringWithCString:"测试" encoding:NSUTF8StringEncoding]; // testSend.ccEmail = @"抄送人邮箱"; // 可不填 testSend.wantsSecure = YES; testSend.delegate = self; NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8;\nThis is a test Email",kSKPSMTPPartContentTypeKey, @"This is a test message.",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; testSend.parts = [NSArray arrayWithObjects:plainPart, nil]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [testSend send]; }); }
// 回调方法
- (void)messageSent:(SKPSMTPMessage *)message { NSLog(@"messageSuccessed-----%@", message); } - (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error { NSLog(@"messageFailed-----message - %@\nerror - %@", message, error); }
4、Tip:关于relayHost参数:smtp.ym.163.com(网易企业邮箱)smtp.163.com(网易邮箱)。具体其他邮箱服务器地址查看帮助中心。