Python imaplib login failed

9.2k views Asked by At

I'm working on mail application and encountering a big problem with Python imaplib.

Here is the code that I try to use to login to mail server.

M = imaplib.IMAP4(self.server,int(self.port))
M.login(self.user, self.password)

I'm using "port 143", "useSLL = false", no secure.
And here is the message that I receive when trying to login to server.

DEBUG:root:Login failed.
Traceback (most recent call last):
  File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 347, in getMail
    M.login(self.user, self.password)
  File "/opt/splunk/lib/python2.7/imaplib.py", line 520, in login
    raise self.error(dat[-1])
error: Login failed.
None
Traceback (most recent call last):
  File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 724, in <module>
    parseArgs()
  File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 716, in parseArgs
    imapProc.getMail()
  File "/opt/splunk/etc/apps/IMAPmailbox/bin/get_imap_email.py", line 352, in getMail
    raise LoginError('Could not log into server: %s with password provided' % self.server)
__main__.LoginError: Could not log into server: (my server) with password provided

p/s: I have another mail application which get emails throw imap on ruby and it's working fine with port 143 no secure.

Anyone please help me to solve this problem. Thanks

2

There are 2 answers

0
Tauqeer Afzal On

I got a solution which work for both gmail and Outlook

def connect(self, username, password): if self.tls: self.server.starttls()

    if(self.hostname == 'imap.outlook.com'):
        imap_server = "outlook.office365.com"
        self.server = self.transport(imap_server, self.port)
        self.server = imaplib.IMAP4_SSL(imap_server)
        (retcode, capabilities) = self.server.login(username,password)
        self.server.select('inbox')
    else:
        typ, msg = self.server.login(username, password)
        if self.folder:
            self.server.select(self.folder)
        else:
            self.server.select()
3
Jatin Mahajan On

Use this instead

M = imaplib.IMAP4_SSL(self.server,int(self.port)) M.login(self.user, self.password)

I am using imaplib.IMAP4_SSL instead of imaplib.IMAP4and this seems to work for me