ios发送邮件,第三方库SKPSMTPMessage发送邮件的使用. 不需要调用系统的邮件,可以在app后台发送邮件.下面是整理的一个使用SKPSMTPMessage方法.
//
// NISendMail.m
//
// Created by 罗若文 on 16/7/7.
// Copyright © 2016年 罗若文. All rights reserved.
//
#import "NISendMail.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
@interface NISendMail ()
@property NSString * login;
@property NSString * password;
@property NSString * relayHost;
@end
@implementation NISendMail
#pragma mark - 初始化,发送者邮箱,密码,邮箱服务器
- (instancetype)initWithEmail:(NSString *)login password:(NSString *)password relayHost:(NSString *)relayHost
{
self = [super init];
if (self) {
_login=login;
_password=password;
_relayHost=relayHost;
}
return self;
}
#pragma mark - 发送邮件:标题,接收方,内容,附件
-(void)sendMail:(NSString *)title toEmail:(NSString *)toEmail content:(NSString *)content enclosureName:(NSString *)enclosureName enclosure:(NSData * )enclosure{
[self sendMail:title toEmail:toEmail bccEmail:nil ccEmail:nil content:content enclosureName:enclosureName enclosure:enclosure];
}
#pragma mark - 发送邮件:标题,接收方,隐藏抄送,抄送,内容,附件名,附件
-(void)sendMail:(NSString *)title toEmail:(NSString *)toEmail bccEmail:(NSString *)bccEmail ccEmail:(NSString *)ccEmail content:(NSString *)content enclosureName:(NSString *)enclosureName enclosure:(NSData * )enclosure{
if(!content){
content=@" ";
}
if([NIString isEmpty:enclosureName]){
enclosureName=@"nullName.config";//忘记传名字的时候
}
//线程发送邮件
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
testMsg.fromEmail = _login;
testMsg.toEmail = toEmail;
testMsg.relayHost = _relayHost;//@"smtp.qq.com";
testMsg.requiresAuth = [[NSNumber numberWithBool:YES] boolValue];
if (testMsg.requiresAuth) {
testMsg.login = _login;
testMsg.pass = _password;
}
testMsg.wantsSecure = [[NSNumber numberWithBool:YES] boolValue]; // smtp.gmail.com doesn't work without TLS!
if(title){
testMsg.subject = title; //主题
}else{
testMsg.subject = @"标题"; //主题
}
if(bccEmail){
testMsg.bccEmail = bccEmail; //隐藏抄送
}
if(ccEmail){
testMsg.ccEmail = ccEmail; //抄送
}
NSDictionary * plainPart = [NSDictionary dictionaryWithObjectsAndKeys:
@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
content,kSKPSMTPPartMessageKey,
@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
if(enclosureName && enclosure){
NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"%@\"",enclosureName],kSKPSMTPPartContentTypeKey,
[NSString stringWithFormat:@"attachment;\r\n\tfilename=\"%@\"",enclosureName],kSKPSMTPPartContentDispositionKey,
[enclosure encodeBase64ForData],kSKPSMTPPartMessageKey,
@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];
}
else{
testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
}
[testMsg send];
});
}
@end
在需要发送邮件的地方可以这样写
NISendMail * sm=[[NISendMail alloc]initWithEmail:@"写发送者邮箱" password:@"发送者密码,如果是qq邮箱要用独立密码" relayHost:@"smtp.qq.com"];
[sm sendMail:@"标题" toEmail:@"732649784@qq.com" content:@"内容测试:使用第三方库SKPSMTPMessage发送邮件" enclosureName:nil enclosure:nil];