How to get message body and last email in mailcore2?

849 Views Asked by At

I want to receive message body and the last received email or unseen emails in Swift 4. I search about it but every code in Objective-C or any other old languages. In Gmail, Yahoo, Outlook.

I received emails but I not successful to get the body of the message, and the last known message received.

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!{

I received all emails who are in my inbox but I want only unseen messages. And main problem is that I not successful to get the message body.

1

There are 1 best solutions below

2
Maaz Siddiqui On BEST ANSWER
   var session = MCOIMAPSession() // Make Global
    session.hostname = "imap.gmail.com"
    session.port = 993
    session.username = "[email protected]"
    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)
    }   
}