How to fetch the inbox from email agent in swift/objective c using mailcore framework or Any framework?

1.9k Views Asked by At

Step 1: Link (https://github.com/MailCore/MailCore2)

Step 2: I have added mailcore-framework in my project

Step 3: pod install for UICKeyChainStore in my project done

Step 4: Send mail successfully using MCOSMTPSession, MCOMessageBuilder.

Step 5: My problem is that I am not able to fetch using mailcore. Is there any other framework for fetching mail (inbox)?

2

There are 2 best solutions below

0
Mario Muniz On

You can give https://github.com/snipsco/Postal a try. This is a framework which aims to provide simple access to common email providers.

5
Julio César Arregoitía Val On

Sorry for late answer. I have two apps in the App Store, both of them use MailCore2, so I can explain you a thing or two about that.

Of course you can fetch the emails with MailCore2, this is the code. If you have any other doubt with MailCore2 write about that, I will try to find the answer.

var imapsession:MCOIMAPSession = MCOIMAPSession()

func prepareImapSession() 
{
    // CONFIGURE THAT DEPENDING OF YOUR NEEDS
    imapsession.hostname = imapHostname // String
    imapsession.username = userName // String
    imapsession.password = password // String
    imapsession.port = portIMAP // 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

                    // THE uid for this email. The uid is unique for one email
                    let uid = msgs[i].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
                    
                }
            }
     }


 // 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
            
        // FOR THE MESSAGEPARSER YOU CAN EPLORE MORE THAN ONE OPTION TO OBTAIN THE TEXT
        let msgPlainBody = messageParser.plainTextBodyRendering()
        
   }