IOS使用mailcore2发送邮件,qq邮箱发送

阅读:1083 2019-03-20 14:09:32 来源:新网

1.首先获取mailcore2的代码

gitclonehttps://github.com/mailcore/mailcore2/

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

3.将静态库添加到工程中(1)在linkbinarywithlibraries中添加:

libmailcore-ios.a

(2)在targetdependencies中添加:

staticmaincore2ios

(3)在linkbinarywithlibraries中添加系统包:

cfnetwork.frameworksecurity.framework

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

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

(2)在c++standardlibrary选择:

libc++

5.选择target编译静态包

mailcoreiosstaticmailcore2ios

6.添加环境变量选择product->scheme->editscheme->run->arguments->environmentvariables添加os_activity_mode值威disable二。我们使用smtp来发送邮件1.首先编辑发送参数,调用登陆校验方法

-(void)mailcorelogin{_smtpsession=[[mcosmtpsessionalloc]init];_smtpsession.hostname=@"smtp.qq.com";//qq邮箱地址_smtpsession.port=587;//qq邮箱端口号_smtpsession.username=@"xxxxxxxx@qq.com";_smtpsession.password=@"xxxxxxxx";//qq邮箱开启smtp服务之后的**授权码**不是邮箱原始的密码_smtpsession.connectiontype=mcoconnectiontypestarttls;//httpsmcosmtpoperation*smtpoperation=[self.smtpsessionloginoperation];[smtpoperationstart:^(nserror*error){if(error==nil){nslog(@"loginsuccessed");}else{nslog(@"loginfailure:%@",error);}}];}

二,设置发送具体信息

-(void)buildmessage{//构建邮件体的发送内容mcomessagebuilder*messagebuilder=[[mcomessagebuilderalloc]init];messagebuilder.header.from=[mcoaddressaddresswithdisplayname:@"张三"mailbox:@"422301261@qq.com"];//发送人messagebuilder.header.to=@[[mcoaddressaddresswithmailbox:@"164792796@qq.com"]];//收件人(多人)//messagebuilder.header.cc=@[[mcoaddressaddresswithmailbox:@"@333333qq.com"]];//抄送(多人)//messagebuilder.header.bcc=@[[mcoaddressaddresswithmailbox:@"444444@qq.com"]];//密送(多人)messagebuilder.header.subject=@"测试邮件";//邮件标题messagebuilder.textbody=@"helloworld";//邮件正文/*如果邮件是回复或者转发,原邮件中往往有附件以及正文中有其他图片资源,如果有需要你可将原文原封不动的也带过去,这里发送的正文就可以如下配置*/nsstring*bodyhtml=@"

我是原邮件正文

";nsstring*body=@"我是邮件回复的内容";nsmutablestring*fullbodyhtml=[nsmutablestringstringwithformat:@"%@
-------------原始邮件-------------
%@",[bodystringbyreplacingoccurrencesofstring:@"n"withstring:@"
"],bodyhtml];[messagebuildersethtmlbody:fullbodyhtml];[selfsendmessage:messagebuilder];}-(void)sendmessage:(mcomessagebuilder*)messagebuilder{nsdata*rfc822data=[messagebuilderdata];mcosmtpsendoperation*sendoperation=[self.smtpsessionsendoperationwithdata:rfc822data];[sendoperationstart:^(nserror*error){if(error==nil){nslog(@"sendsuccessed");}else{nslog(@"sendfailure:%@",error);}}];}

三。一些问题1.errordomain=mcoerrordomaincode=5“unabletoauthenticatewiththecurrentsession’scredentials.”userinfo={nslocalizeddescription=unabletoauthenticatewiththecurrentsession’scredentials.}这个错误主要是邮箱帐号或者密码错误。邮箱后缀是否正确(qq邮箱填的密码就是发送短信之后显示的授权码,而不是直接填原始密码)2.errordomain=mcoerrordomaincode=1“astableconnectiontotheservercouldnotbeestablished.”userinfo={nslocalizeddescription=astableconnectiontotheservercouldnotbeestablished.}这个错误可以从以下三个方面尝试解决:1.hostname不存在,或者拼写错误,修改hostname;该hostname不一定是imap.**.com,我项目中用到的hostname是10.101.10.1(主机名)。2.需要ssl安全链接,将connectiontype设置为mcoconnectiontypetls;3.端口号错误。

相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服