Sounak Banerjee
Sounak Banerjee

Reputation: 99

Unable to connect to gmail/Outlook using IMAP in python

I have tried to connect to the Gmail server using IMAP in Spyder(Python 3.6) using the Chilkat package. I have enabled the IMAP for all Mail in the Settings>Forwarding and POP/IMAP and then I have also enabled the less secure apps tab here https://myaccount.google.com/lesssecureapps?pli=1 after signing in. But in this code

import sys
import chilkat

imap = chilkat.CkImap()

#  Anything unlocks the component and begins a fully-functional 30-day trial.
success = imap.UnlockComponent("Anything for 30-day trial")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

#  Connect to an IMAP server.
#  Use TLS
imap.put_Ssl(True)
imap.put_Port(993)
success = imap.Connect("imap.gmail.com")

The success variable which is a boolean remains False. Please help me. My aim is to fetch all attachments from Outlook Server and dump them into a file.But I cannot even connect to the Gmail server. I tried to use "imap.mail.Outlook.com" but that also failed. I do not know the steps to enable IMAP in Outlook. But even if it is enabled in Gmail, why is it not working?

Upvotes: 0

Views: 1262

Answers (1)

Chilkat Software
Chilkat Software

Reputation: 1659

The 1st step is to examine the contents of the imap.LastErrorText property to see what happened. For example:

#  Connect to an IMAP server.
#  Use TLS
imap.put_Ssl(True)
imap.put_Port(993)
success = imap.Connect("imap.someMailServer.com")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

My guess is that a firewall (software or hardware) is blocking the outbound connection.

An alternative solution is to use the GMail REST API as shown in these examples: https://www.example-code.com/python/gmail.asp The HTTP ports (443) are unlikely to be blocked by a firewall. You would download into a Chilkat Email object and then save attachments in exactly the same way had you downloaded via IMAP.

Upvotes: 1

Related Questions