how to fetch emails using Mailcore2 in ios swift?

636 Views Asked by At

I am trying to fetch the emails using mailcore2 in my ios app, I got this errors

2020-11-02 11:33:40.118418+0400 TestReadEmailsApp[3390:81780] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed 2020-11-02 11:33:48.984191+0400 TestReadEmailsApp[3390:81644] Unable to authenticate with the current session's credentials.

Here my code that I tried

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    prepareImapSession()
    useImapWithUIDS()
}

var imapsession:MCOIMAPSession = MCOIMAPSession()

func prepareImapSession()
{
    // CONFIGURE THAT DEPENDING OF YOUR NEEDS
    imapsession.hostname = "imap.gmail.com" // String
    imapsession.username = "[email protected]" // String
    imapsession.password = "************" // String
    imapsession.port = 993 // 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
                    print(" MAILS subjects ====================== ", subject ?? "no vv")
                    // THE uid for this email. The uid is unique for one email
                    let uid = msgs[i].uid
                    print(" MAILS uid ======== ", 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
                    print(" MAILS seq num ======== ", 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
        print(" MAILS attach data ========== ", attachData!)
        // FOR THE MESSAGEPARSER YOU CAN EPLORE MORE THAN ONE OPTION TO OBTAIN THE TEXT
        let msgPlainBody = messageParser.plainTextBodyRendering()
        print(" MAILS msg body =========== ", msgPlainBody!)
   }
}
}
    
}

I know that I might did something wrong, I found this code from this question here, so please help me to solve this and thanks in advance

0

There are 0 best solutions below