Objective

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];

查看源码:http://git.oschina.net/shareDemoCode/SendEmailDemo

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

sprintboot

一、邮件发送使用springboot自带的邮件系统就能实现邮件的发送,首先导入依赖:1、新建springboot项目,添加依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstartermail

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

SpringBoot入门 (十) 发送邮件

本文记录学习在SpringBoot中发送邮件。一邮件发送过程发送邮件是一个我们在项目中经常会用到的功能,如在用户注册时发送验证码,账户激活等都会用到。完整的一个邮件发送过程主要包含以下几个步骤:1发件人在用户邮件代理上写邮件内容及收件人的邮箱地址;2用户邮件代理根据发件人填写的邮件信息,生成一封符合邮件格式的邮件;