IOS使用mailcore2发送邮件,qq邮箱发送_airmail unable to authenticate with the current se-程序员宅基地

技术标签: IOS笔记  qq邮箱  邮件  ios  

1.首先获取mailcore2的代码

git clone https://github.com/MailCore/mailcore2/

2.将下载的代码拷贝到项目文件目录下,打开自己的工程将下载的mailcore2工程文件mailcore2.xcodeproj直接拉到自己的工程下面
下载中的目录为build-mac/mailcore2.xcodeproj

3.将静态库添加到工程中
(1)在 Link Binary With Libraries 中添加:

libMailCore-ios.a  

(2)在 Target Dependencies 中添加:

static maincore2 ios 

(3)在 Link Binary With Libraries 中添加系统包:

CFNetwork.framework  
Security.framework  

4.设置编译链接选项
(1)在other linker flag 中添加:

-lctemplate-ios -letpan-ios -lxml2 -lsasl2 -liconv -ltidy -lz -lc++ -stdlib=libc++ -ObjC -lresolv  

(2)在C++ Standard Library选择:

libc++

5.选择target编译静态包

mailcore ios
static mailcore2 ios

6.添加环境变量
选择product->scheme->edit scheme->Run->Arguments->Environment Variables添加OS_ACTIVITY_MODE值威disable
二。我们使用SMTP来发送邮件
1.首先编辑发送参数,调用登陆校验方法

- (void)mailCorelogin
{
    _smtpSession = [[MCOSMTPSession alloc] init];
    _smtpSession.hostname = @"smtp.qq.com";//qq邮箱地址
    _smtpSession.port = 587;//qq邮箱端口号
    _smtpSession.username = @"[email protected]";
    _smtpSession.password = @"xxxxxxxx";//qq邮箱开启SMTP服务之后的**授权码**不是邮箱原始的密码
    _smtpSession.connectionType = MCOConnectionTypeStartTLS;//https


    MCOSMTPOperation *smtpOperation = [self.smtpSession loginOperation];
    [smtpOperation start:^(NSError * error) {
        if (error == nil) {
            NSLog(@"login successed");
        } else {
            NSLog(@"login failure: %@", error);
        }  
    }];
}

二,设置发送具体信息

-(void)buildMessage
{
    // 构建邮件体的发送内容
    MCOMessageBuilder *messageBuilder = [[MCOMessageBuilder alloc] init];
    messageBuilder.header.from = [MCOAddress addressWithDisplayName:@"张三" mailbox:@"[email protected]"];   // 发送人
    messageBuilder.header.to = @[[MCOAddress addressWithMailbox:@"[email protected]"]];       // 收件人(多人)
//    messageBuilder.header.cc = @[[MCOAddress addressWithMailbox:@"@333333qq.com"]];      // 抄送(多人)
//    messageBuilder.header.bcc = @[[MCOAddress addressWithMailbox:@"[email protected]"]];    // 密送(多人)
    messageBuilder.header.subject = @"测试邮件";    // 邮件标题
    messageBuilder.textBody = @"hello world";           // 邮件正文

    /*
     如果邮件是回复或者转发,原邮件中往往有附件以及正文中有其他图片资源,
     如果有需要你可将原文原封不动的也带过去,这里发送的正文就可以如下配置
     */
    NSString * bodyHtml = @"<p>我是原邮件正文</p>";
    NSString *body = @"我是邮件回复的内容";
    NSMutableString*fullBodyHtml = [NSMutableString stringWithFormat:@"%@<br/>-------------原始邮件-------------<br/>%@",[body stringByReplacingOccurrencesOfString:@"\n"withString:@"<br/>"],bodyHtml];
    [messageBuilder setHTMLBody:fullBodyHtml];
    [self sendMessage:messageBuilder];
}
-(void)sendMessage:(MCOMessageBuilder *)messageBuilder
{
    NSData * rfc822Data =[messageBuilder data];
    MCOSMTPSendOperation *sendOperation = [self.smtpSession sendOperationWithData:rfc822Data];
    [sendOperation start:^(NSError *error) {
        if (error == nil) {
            NSLog(@"send successed");
        } else {
            NSLog(@"send failure: %@", error);
        }  
    }];
}

三。一些问题
1.Error Domain=MCOErrorDomain Code=5 “Unable to authenticate with the current session’s credentials.” UserInfo={NSLocalizedDescription=Unable to authenticate with the current session’s credentials.}
这个错误主要是邮箱帐号或者密码错误。邮箱后缀是否正确(qq邮箱填的密码就是发送短信之后显示的授权码,而不是直接填原始密码)
2.Error Domain=MCOErrorDomain Code=1 “A stable connection to the server could not be established.” UserInfo={NSLocalizedDescription=A stable connection to the server could not be established.}
这个错误可以从以下三个方面尝试解决:
1.hostname不存在,或者拼写错误,修改hostname;该hostname不一定是 imap.**.com,我项目中用到的hostname是 10.101.10.1(主机名)。
2.需要SSL安全链接,将connectionType设置为MCOConnectionTypeTLS;
3.端口号错误。

参考文献:
1,http://blog.csdn.net/qq510304723/article/details/50150303
2.https://www.jianshu.com/p/558b3bd9f88d

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ios_lujian/article/details/78875046

智能推荐

简化VUE路由_vue @/views-程序员宅基地

文章浏览阅读340次。当页面很多时,vue路由也会随之增多,从而增加代码量。生产环境中的路由建议使用懒加载模式开发环境中的路由不使用懒加载,会造成webpack热更新缓慢先在router文件夹下建立三个文件,index.js、import-development.js、import-production.jsindex.js//普通路由const routes = [ { path: '/login', name: 'login', componen_vue @/views

教父2-英文版-程序员宅基地

文章浏览阅读2k次。MARIO PUZO'S THE GODFATHER PART IIby Mario Puzo and Francis Ford CoppolaFADE FROM BLACK TO: MICHAEL, in profile looking downward. He holds out his hand and ROCCO kissing it.DISSOLVE TO: A ..._教父2英文版

pydot_ng.InvocationException:Program terminated with status:1. stderr follows: Format:“ps” not ……_invocationexception: program terminated with statu-程序员宅基地

文章浏览阅读5.5k次,点赞26次,收藏9次。在曲折地安装完graphviz之后,运行代码时出现这个问题(安装graphviz的教程:https://blog.csdn.net/wyx100/article/details/80253072or https://blog.csdn.net/Snowy_susu/article/details/90439423?ops_request_misc=&request_id=&..._invocationexception: program terminated with status: 1. stderr follows: form

springboot2.2整合spring-data-elasticsearch3.2_spring data elasticsearch health check close sprin-程序员宅基地

文章浏览阅读4k次。环境:Elasticsearch:7.4.1Springboot:2.2.1Spring-data-elasticsearch:3.2.0IDE:STS_3.9.2.RELEASEpom.xml配置<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0..._spring data elasticsearch health check close spring 3.2

图形化编程Mixly——RFID智能门禁_csdnrfidmixly-程序员宅基地

文章浏览阅读2.4k次,点赞2次,收藏11次。文章目录1.软硬件连接2.图形化编程块3.代码块4.实验成果实验材料与环境1、硬件Arduino开发板、舵机SG90、RFID-RC522读卡器、校园卡、杜邦线若干2、软件Mixly IDE(下载地址:https://pan.baidu.com/s/1vKnY-vC4LU0qMFitArEXfw提取码:tbfe)实验内容【1】读取校园卡ID号。【2】读取到指定校园卡时S..._csdnrfidmixly

java/jsp/ssm基于web的多媒体素材管理系统【2024年毕设】_基于web的多媒体素材管理系统zip-程序员宅基地

文章浏览阅读53次。springboot基于Springboot的企业cms内容管理系统。springboot基于Vue和Springboot的会议室管理系统。开发软件:eclipse/myeclipse/idea。springboot中小型企业物流管理系统的设计与实现。springboot微信小程序的食谱大全“食全食美”springboot基于微信小程序的舟袍设计工作室。ssm基于web的佳茗天香茶品销售平台的设计与实现。springboot医考答题练习系统的设计与实现。springboot微信小程序的火锅店点餐系统。_基于web的多媒体素材管理系统zip

随便推点

成功解决Failed to import transformers.models.vision_encoder_decoder.modeling_vision_encoder_decoder beca_runtimeerror: failed to import modelscope.utils.hf-程序员宅基地

文章浏览阅读1.9k次,点赞3次,收藏3次。​成功解决Failed to import transformers.models.vision_encoder_decoder.modeling_vision_encoder_decoder because of the following error (look up to see its traceback):目录解决问题解决思路解决方法解决问题RuntimeError: Failed to import transformers.models.vision_encoder_d_runtimeerror: failed to import modelscope.utils.hf_util because of the follo

BeanDefinitionRegistryPostProcessor详解-程序员宅基地

文章浏览阅读8.8k次,点赞5次,收藏22次。接口该接口继承了BeanFactoryPostProcessor接口,此接口中只有一个方法,就是postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)代码public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor { /** * Modify the application context's intern_beandefinitionregistrypostprocessor

Mac m2 Ventura(13.4)安装Cocoapods_mac m2 安装cocoapods-程序员宅基地

文章浏览阅读2.1k次,点赞3次,收藏7次。报错:You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory。或者配置 hosts 185.199.108.133 raw.githubusercontent.com。3.使用ruby 版本管理安装 新的ruby。2.安装新的homebrew(参考 官网。非常不推荐 sudo 安装!6.不需要 sudo。_mac m2 安装cocoapods

PL2303驱动安装需要联网_ztekdriver_pl2303-程序员宅基地

文章浏览阅读255次。在使用PL2303驱动时,需要连接网络。例如:USB-RS232插入电脑后会在windows10系统设备管理中的其他设备中显示USE-Ser!这个表示没有安装驱动,我安装了PL2303驱动后也没办法使用,后来在网络连接之后等待大约10分钟后可用。_ztekdriver_pl2303

Android P Camera架构_android camera2 id 103-程序员宅基地

文章浏览阅读3.1k次。一、APP层打开摄像头:openCamera();二、frameworks层:CameraManager:代码路径:\frameworks\base\core\java\android\hardware\camera2\CameraManager.javaprivate CameraDevice openCameraDeviceUserAsync(String cameraId, ..._android camera2 id 103

米家接入HomeKit系列二:通过群辉NAS的Docker搭建HomeAssistant_群晖接入米家-程序员宅基地

文章浏览阅读1.1w次,点赞5次,收藏41次。通过前面的文章我们已经知道我们为什么要搭建HomeAssistant,那么本篇文章我们就来给大家讲解如何通过群辉NAS的Docker搭建HomeAssistant,以及其基本的配置和使用。_群晖接入米家

推荐文章

热门文章

相关标签