Muhammad Umar
Muhammad Umar

Reputation: 1

How to fetch body of email using Mail Core2 IOS Swift

I am building a app which receive emails I fetched the subject of email but i am not able to get the body Email. here is my code

let imapsession = MCOIMAPSession()
imapsession.hostname = "xxxx"
imapsession.port = 993
imapsession.username = "xxxx"
imapsession.password = "xxxx"
imapsession.connectionType = MCOConnectionType.TLS

let requestKind : MCOIMAPMessagesRequestKind = MCOIMAPMessagesRequestKind.Headers
let folder : String = "INBOX"
let uids : MCOIndexSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))

let fetchOperation : MCOIMAPFetchMessagesOperation = imapsession.fetchMessagesOperationWithFolder(folder, requestKind: requestKind, uids: uids)

fetchOperation.start { (err, msg, vanished) -> Void in
let msgs = msg as! [MCOIMAPMessage]
print("error from server \(err)")

for i in 0..<msgs.count
{
    // Here i want the body of all emails fetched...

    if let m = msgs[i].header.subject
    {
        print("\(i). \(m)")
    }
}

}

i have seen this answer as well but i could not understand it

Fetch an email body in mailcore2 OSX with swift

so please guide me about this

Upvotes: 0

Views: 1887

Answers (1)

user5938635
user5938635

Reputation:

Try this code it will help you:

var session: MCOIMAPSession = MCOIMAPSession()
session.hostname = "imap.gmail.com"
session.port = 993
session.username = UICKeyChainStore.stringForKey("username")
session.password = UICKeyChainStore.stringForKey("password")
session.connectionType = MCOConnectionTypeTLS
var operation: MCOIMAPFetchContentOperation = session.fetchMessageByUIDOperationWithFolder("INBOX", uid: message.uid)
operation.start({(error: NSError, data: NSData) -> Void in
    var messageParser: MCOMessageParser = MCOMessageParser(data: data)
    var msgHTMLBody: String = messageParser.htmlBodyRendering()
    webView.loadHTMLString(msgHTMLBody, baseURL: nil)
})

Upvotes: 1

Related Questions