ios发送邮件,第三方库SKPSMTPMessage发送邮件的使用. 不需要调用系统的邮件,可以在app后台发送邮件.下面是整理的一个使用SKPSMTPMessage方法.
1// 2// NISendMail.m 3// 4// Created by 罗若文 on 16/7/7. 5// Copyright © 2016年 罗若文. All rights reserved. 6// 7 8#import "NISendMail.h" 9#import "SKPSMTPMessage.h" 10#import "NSData+Base64Additions.h" 11 12@interface NISendMail () 13@property NSString * login; 14@property NSString * password; 15@property NSString * relayHost; 16@end 17 18@implementation NISendMail 19 20#pragma mark - 初始化,发送者邮箱,密码,邮箱服务器 21- (instancetype)initWithEmail:(NSString *)login password:(NSString *)password relayHost:(NSString *)relayHost 22{ 23 self = [super init]; 24 if (self) { 25 _login=login; 26 _password=password; 27 _relayHost=relayHost; 28 } 29 return self; 30} 31 32#pragma mark - 发送邮件:标题,接收方,内容,附件 33-(void)sendMail:(NSString *)title toEmail:(NSString *)toEmail content:(NSString *)content enclosureName:(NSString *)enclosureName enclosure:(NSData * )enclosure{ 34 [self sendMail:title toEmail:toEmail bccEmail:nil ccEmail:nil content:content enclosureName:enclosureName enclosure:enclosure]; 35} 36 37#pragma mark - 发送邮件:标题,接收方,隐藏抄送,抄送,内容,附件名,附件 38-(void)sendMail:(NSString *)title toEmail:(NSString *)toEmail bccEmail:(NSString *)bccEmail ccEmail:(NSString *)ccEmail content:(NSString *)content enclosureName:(NSString *)enclosureName enclosure:(NSData * )enclosure{ 39 if(!content){ 40 content=@" "; 41 } 42 if([NIString isEmpty:enclosureName]){ 43 enclosureName=@"nullName.config";//忘记传名字的时候 44 } 45 //线程发送邮件 46 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 47 SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init]; 48 testMsg.fromEmail = _login; 49 testMsg.toEmail = toEmail; 50 testMsg.relayHost = _relayHost;//@"smtp.qq.com"; 51 testMsg.requiresAuth = [[NSNumber numberWithBool:YES] boolValue]; 52 if (testMsg.requiresAuth) { 53 testMsg.login = _login; 54 testMsg.pass = _password; 55 } 56 testMsg.wantsSecure = [[NSNumber numberWithBool:YES] boolValue]; // smtp.gmail.com doesn't work without TLS! 57 if(title){ 58 testMsg.subject = title; //主题 59 }else{ 60 testMsg.subject = @"标题"; //主题 61 } 62 63 if(bccEmail){ 64 testMsg.bccEmail = bccEmail; //隐藏抄送 65 } 66 if(ccEmail){ 67 testMsg.ccEmail = ccEmail; //抄送 68 } 69 70 NSDictionary * plainPart = [NSDictionary dictionaryWithObjectsAndKeys: 71 @"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey, 72 content,kSKPSMTPPartMessageKey, 73 @"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; 74 75 76 if(enclosureName && enclosure){ 77 NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys: 78 [NSString stringWithFormat:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"%@\"",enclosureName],kSKPSMTPPartContentTypeKey, 79 [NSString stringWithFormat:@"attachment;\r\n\tfilename=\"%@\"",enclosureName],kSKPSMTPPartContentDispositionKey, 80 [enclosure encodeBase64ForData],kSKPSMTPPartMessageKey, 81 @"base64",kSKPSMTPPartContentTransferEncodingKey,nil]; 82 83 testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil]; 84 } 85 else{ 86 testMsg.parts = [NSArray arrayWithObjects:plainPart,nil]; 87 } 88 [testMsg send]; 89 }); 90 91} 92@end
在需要发送邮件的地方可以这样写
1NISendMail * sm=[[NISendMail alloc]initWithEmail:@"写发送者邮箱" password:@"发送者密码,如果是qq邮箱要用独立密码" relayHost:@"smtp.qq.com"]; 2 [sm sendMail:@"标题" toEmail:@"732649784@qq.com" content:@"内容测试:使用第三方库SKPSMTPMessage发送邮件" enclosureName:nil enclosure:nil];