如何使用 mailcore 框架或任何框架从 swift/objective c 中的电子邮件代理中获取收件箱?
Posted
技术标签:
【中文标题】如何使用 mailcore 框架或任何框架从 swift/objective c 中的电子邮件代理中获取收件箱?【英文标题】:How to fetch the inbox from email agent in swift/objective c using mailcore framework or Any framework? 【发布时间】:2018-09-26 02:23:51 【问题描述】:第 1 步:链接 (https://github.com/MailCore/MailCore2)
第 2 步:我在项目中添加了 mailcore-framework
第 3 步:完成项目中 UICKeyChainStore 的 pod 安装
第 4 步:使用 MCOSMTPSession、MCOMessageBuilder 成功发送邮件。
第 5 步:我的问题是我无法使用 mailcore 获取。是否有任何其他框架用于获取邮件(收件箱)?
【问题讨论】:
【参考方案1】:很抱歉这么晚才回复你,但我是 *** 的新手,但我必须在 App Store 中使用应用程序,它们都使用 MailCore2,所以我可以向你解释一两件事。
当然您可以使用 MailCore2 获取电子邮件,这是代码。如果您对 MailCore2 有任何其他疑问,请写下,我会尝试找到问题。
var imapsession:MCOIMAPSession = MCOIMAPSession()
func prepareImapSession()
// CONFIGURE THAT DEPENDING OF YOUR NEEDS
imapsession.hostname = imapHostname // String
imapsession.username = userName // String
imapsession.password = password // String
imapsession.port = portIMAP // UInt32 number
imapsession.authType = MCOAuthType.saslLogin
imapsession.connectionType = MCOConnectionType.TLS
func useImapWithUIDS()
// There is more than one option here, explore depending of your needs
let requestKind : MCOIMAPMessagesRequestKind = .headers
let folder : String = "INBOX"
// HERE ALSO EXPLORE DEPENDING OF YOUR NEEDS, RANGE IT IS THE RANGE OF THE UIDS THAT YOU WANT TO FETCH, I SUGGEST TO YOU TO CHANGE THE // NUMBER ONE IF YOU HAVE A LOWER BOUND TO FETCH EMAIL
let uids : MCOIndexSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))
let fetchOperation = imapsession.fetchMessagesOperation(withFolder: folder, requestKind: requestKind, uids: uids)
fetchOperation?.start
(err, msg, vanished) -> Void in
if (err != nil)
error = err
NSLog((err?.localizedDescription)!)
else
guard let msgs = msg as? [MCOIMAPMessage]
else
print("ERROR GETTING THE MAILS")
return
for i in 0..<msgs.count
// THE SUBJECT
let subject = msgs[i].header.subject
// THE uid for this email. The uid is unique for one email
let uid = msgs[i].uid
// The sequenceNumber like the nomber say it is the sequence for the emails in the INBOX from the first one // (sequenceNumber = 1) to the last one , it not represent always the same email. Because if you delete one email then //next one will get the sequence number of that email that was deleted
let sequenceNumber = msgs[i].sequenceNumber
// MARK: - EXTRACT THE CONTENT OF ONE EMAIL, IN THIS FUNCTION YOU NEED THE uid, THE UNIQUE NUMBER FOR ONE EMAIL
func useImapFetchContent(uidToFetch uid: UInt32)
let operation: MCOIMAPFetchContentOperation = imapsession.fetchMessageOperation(withFolder: "INBOX", uid: uid)
operation.start (Error, data) in
if (Error != nil)
NSLog("ERROR")
return
let messageParser: MCOMessageParser = MCOMessageParser(data: data)
// IF YOU HAVE ATTACHMENTS USE THIS
let attachments = messageParser.attachments() as? [MCOAttachment]
// THEN YOU NEED THIS PROPERTIE, IN THIS EXAMPLE I TAKE THI FIRST, USE WHAT EVER YOU WANT
let attachData = attachments?.first?.data
// FOR THE MESSAGEPARSER YOU CAN EPLORE MORE THAN ONE OPTION TO OBTAIN THE TEXT
let msgPlainBody = messageParser.plainTextBodyRendering()
【讨论】:
当我写这行时它显示错误 imapsession.connectionType = MCOAuthType.saslLogin 无法将类型“MCOAuthType”的值分配给类型“MCOConnectionType” 还有一个问题,当我们调用 useImapWithUIDS() 时我应该传递什么参数,我的意思是 startPoint 和 whatProtocol 是什么? 你是对的,这两个参数都超过了我的项目。wProtocol
是我要查找的主题的名称,start
属于参数 MCORangeMake(start, UINT64_MAX)
。 MCORangeMake
用于根据uid
向服务器询问要请求的电子邮件范围
@RoohAl-mahaba 你也是对的,比如imapsession.connectionType = MCOConnectionType.TLS
感谢您为我回复,请您回答我的问题,我发布了一个关于此的问题,我的代码不起作用,***.com/questions/64641790/…【参考方案2】:
你可以试试https://github.com/snipsco/Postal。这是一个旨在提供对常见电子邮件提供商的简单访问的框架。
【讨论】:
以上是关于如何使用 mailcore 框架或任何框架从 swift/objective c 中的电子邮件代理中获取收件箱?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 info.plist 获取静态框架的版本?或任何其他方式?
如何在应用程序的任何部分从 shiro 框架获取 cacheManager