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

How to get message body and last email in 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!{

我收到了收件箱中的所有电子邮件,但我只想要未看到的邮件。主要问题是我没有成功获取消息正文。

   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)
    }   
}