如何在mailcore2中获取消息正文和最后一封电子邮件?

时间:2019-05-18 18:31:59

标签: swift mailcore2

我想在Swift 4中接收消息正文以及最近收到的电子邮件或看不见的电子邮件。我搜索它,但是搜索的是Objective-C或任何其他旧语言的每个代码。在Gmail,Yahoo,Outlook中。

我收到了电子邮件,但没有成功获得邮件的正文以及最后收到的邮件。

var uidSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))

var fetchOp: MCOIMAPFetchMessagesOperation? = session.fetchMessagesByUIDOperation(withFolder: "INBOX", requestKind: MCOIMAPMessagesRequestKind.headers, uids: uidSet)

fetchOp?.start({ err, msgs, vanished in
    //print(msgs)
    let msgs = msgs as? [MCOIMAPMessage]

    for msgs in msgs!{

我收到了收件箱中的所有电子邮件,但只希望收到看不见的邮件。主要问题是我无法成功获取消息正文。

1 个答案:

答案 0 :(得分:0)

   var session = MCOIMAPSession() // Make Global
    session.hostname = "imap.gmail.com"
    session.port = 993
    session.username = "example@gmail.com"
    session.password = "**********"
    session.connectionType = MCOConnectionType.TLS
    let requestKind: MCOIMAPMessagesRequestKind = [.headers, .flags]

    var uidSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))

    var fetchOp: MCOIMAPFetchMessagesOperation? = session.fetchMessagesByUIDOperation(withFolder: "INBOX", requestKind: MCOIMAPMessagesRequestKind.headers, uids: uidSet)

    fetchOp?.start{( err, msgs, vanished) -> Void in


        //print(msgs)
        let msgs = msgs as? [MCOIMAPMessage]


        for msgs in msgs!{

            // THE uid for this email. The uid is unique for one email
            let uiid = msgs.uid

            useImapFetchContent(uidToFetch: uiid)

        }
    }

func useImapFetchContent(uidToFetch uid: UInt32)
{

    let operation = session.fetchParsedMessageOperation(withFolder: "INBOX", uid: UInt32(uid))

    operation?.start{( error, messageParser)-> Void in
        if error == nil {
            let returnValue = messageParser!.plainTextBodyRenderingAndStripWhitespace(false)
        }
        let subject = messageParser?.header.subject
        print(subject)
        let from = messageParser?.header.from
        print(from)
        let to = messageParser?.header.to
        print(to)
        let msgPlainBody = messageParser?.plainTextBodyRendering()
        print("BODY")
        print(msgPlainBody)
    }   
}