Avinash Sharma
Avinash Sharma

Reputation: 675

Mailcore 2 unable to fetch messages in iOS swift

I am trying to fetch last 10 messages from my gmail account INBOX using the following code, but I an unable to do the same.

Find the code below

    let session = MCOIMAPSession()
    session.hostname = "imap.gmail.com"
    session.port = 993
    session.username = ""
    session.password = ""
    session.connectionType = .TLS
    
    
    
    let folder = "INBOX" // The folder you want to fetch emails from
    
    let requestKind: MCOIMAPMessagesRequestKind = [.headers, .flags, .internalDate]
    let uids = MCOIndexSet(range: MCORangeMake(1, 10))
    
    let fetchOperation = session.fetchMessagesOperation(withFolder: folder, requestKind: requestKind, uids: uids)
    
    fetchOperation?.start { error, messages, _ in
        if let error = error {
            completion(.failure(error))
        } else if let messages = messages {
            self.messages = messages
            completion(.success(messages))
        }
    }

It gives me this error Error fetching emails: A stable connection to the server could not be established.

What am I doing wrong?

Upvotes: 0

Views: 114

Answers (1)

Moses Harding
Moses Harding

Reputation: 165

Generate an app-specific password for your gmail account and use that. You'll need to enable 2fa for your account in order for it to work.

Upvotes: 0

Related Questions