上一篇大致说了下IMAP协议中的简单使用,看到文章有人看,为了我的三个
👨💻👨💻👨💻粉丝,那我继续更新一下,希望对你们有帮助,也做个沟通交流。
- IMAP mailcore2-ios 之一 IMAP
- SMTP
- 登录/用户验证
- 创建邮件体
- 附件添加
- 发送
- 发送成功后
手动
添加到“已发送”
文件夹
SMTP
- 1、登录/用户验证
不论哪种协议,都需要先验证邮箱地址/服务器、密码、端口等,SMTP也一样,你也可以不验证,大不了就是发不出去邮件。。。🤓🤓🤓🤓🤓
创建session,都类似的
self.smtpSession.hostname = session.smtpHost;//smtp.***.com
self.smtpSession.username = session.username;//littlecat@xxx.com.cn
self.smtpSession.password = session.password;//password
self.smtpSession.port = (unsigned int)session.smtpPort;//25、465、587?
self.smtpSession.connectionType = session.smtpIsSSL ? MCOConnectionTypeTLS: MCOConnectionTypeClear;//取决于你的邮件服务器是不是SSL的;
self.smtpSession.authType = MCOAuthTypeSASLLogin;//session校验的类型,这里选的登录,其他还没搞明白(没看。。)😌
校验邮箱信息
MCOSMTPOperation *smtpOperation = [smtpSession loginOperation];
[smtpOperation start:^(NSError * error) {
NSLog(@"finished checking SMTP account.");
if (error == nil) {
//now ,you can send email.
} else {
NSLog(@"error check smtp account: %@", error);
}
}];
- 2、创建邮件
- create message builder;
- set sender(from)、receiver(to)、cc and bcc;
- set body (html body or plain text body);
懒得正经打字了,bite me!🙄🙄🙄🙄🙄🙄🙄🙄🙄🙄
- (void)messageBuildFrom:(NSString *)displayName // 发件人昵称,可以不设置;
mailBox:(NSString *)mail //发件人邮箱地址;
to:(NSArray *)to //收件人(邮箱地址字符串格式,下同);
cc:(NSArray *)cc //抄送人;
bcc:(NSArray *)bcc //密送人;
subject:(NSString *)subject //邮件主题;
textBody:(NSString *)body //邮件正文,我这里是带标签的html文本;
attachs:(NSDictionary *)attachs //附件s(文件名称和NSData格式数据);
htmlBody:(MCOAbstractMessage *)msgParser //转发时的原邮件;
completeHandler:(void(^)())complete
error:(void(^)(NSError *error))fail
{
MCOMessageBuilder *msgBuilder = [[MCOMessageBuilder alloc] init];
//header;
if (displayName != nil) {
msgBuilder.header.from = [MCOAddress addressWithDisplayName:displayName mailbox:mail];
}else{
msgBuilder.header.from = [MCOAddress addressWithMailbox:mail];
}
msgBuilder.header.to = [self mailStringToMCOAddress:to];//字符串格式的邮箱地址,要转成address类;
msgBuilder.header.cc = [self mailStringToMCOAddress:cc];
msgBuilder.header.bcc = [self mailStringToMCOAddress:bcc];
msgBuilder.header.subject = subject;
//body
[msgBuilder setHTMLBody:body];
//------见下
}
- (NSArray *)mailStringToMCOAddress:(NSArray *)arr
{
NSMutableArray *temp = [NSMutableArray array];
for (NSString *str in arr) {
MCOAddress *add = [MCOAddress addressWithMailbox:str];
[temp addObject:add];
}
return temp.copy;
}
- 3、附件处理
- (void)messageBuildFrom:(NSString *)displayName
mailBox:(NSString *)mail
to:(NSArray *)to
cc:(NSArray *)cc
bcc:(NSArray *)bcc
subject:(NSString *)subject
textBody:(NSString *)body
attachs:(NSDictionary *)attachs
htmlBody:(MCOAbstractMessage *)msgParser
completeHandler:(void(^)())complete
error:(void(^)(NSError *error))fail
{
MCOMessageBuilder *msgBuilder = [[MCOMessageBuilder alloc] init];
// -------见上 。。。 ------//
if (msgParser != nil) {
//附加资源,
NSArray *inAttach = msgParser.htmlInlineAttachments;
for (MCOAttachment *attach in inAttach) {
NSString * path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@",msgParser.header.messageID,attach.filename]];
NSData *data = [NSData dataWithContentsOfFile:path];
if (data) {
MCOAttachment *temp = [MCOAttachment attachmentWithData:data filename:attach.filename];
temp.inlineAttachment = YES;
[msgBuilder addRelatedAttachment:temp];
}
}
}
//看接口文档的意思,上面是把转发的邮件中的附件添加上,
//可是并不起作用。。。采取的方法是,下面循环再添加一次。。。。🤧🤧🤧🤧🤧
//附件
if (attachs) {
for (NSString *name in attachs.allKeys) {
MCOAttachment *attach = [MCOAttachment attachmentWithData:[attachs objectForKey:name] filename:name];
[msgBuilder addAttachment:attach];
}
}
//send
//--------见下
}
- 4、发送邮件
邮件也创建了,附件添加完,终于要发送了,但是要注意的是发送操作成功完成后,对方确实可以成功收到,但是在发送方的“已发送”是没有的,需要手动append一份过去
。
执行append操作,需要用imapSession
的,不要搞错了。😅
“已发送”文件夹的filepath,可能是Sent、Sent Messages
等等,具体看你解析的邮箱服务器,不一定都是Sent 哦。
//send
NSData *rfc822 = [msgBuilder data];
MCOSMTPSendOperation *send = [smtpSession sendOperationWithData:rfc822];
[send start:^(NSError * _Nullable error) {
if (error) {
fail(error);
}else {
//发送成功后,手动添加到“已发送”;
MCOIMAPAppendMessageOperation *op = [imapSession appendMessageOperationWithFolder:[MMIMAPTool getVirtualNameFor:@"Sent"] messageData:rfc822 flags:MCOMessageFlagMDNSent];
[op start:^(NSError *error, uint32_t createdUID) {
NSLog(@"create sent message :%@\nerror:%@", @(createdUID), error);
}];
complete();
}
}];
注:上一篇IMAP使用中提到过,创建草稿,过程和这个类似,不过最后执行append 是 放到 草稿箱
,不是已发送
了。
- POP 还没写
网友评论