1

I want to check gmail's mail.

I wrote this code with python.

import imaplib
import email

UserName = "my_gmail"
PassName = "my_gmail_password"

gmail = imaplib.IMAP4_SSL("imap.gmail.com", '993')
gmail.login(UserName, PassName)
gmail.select("")

head, data = gmail.search(None, 'ALL')

but occured this error.

error Traceback (most recent call last) in 8 9 gmail = imaplib.IMAP4_SSL("imap.gmail.com", '993') ---> 10 gmail.login(UserName, PassName) 11 gmail.select("") 12

~/.pyenv/versions/3.7.4/lib/python3.7/imaplib.py in login(self, user, password) 596 typ, dat = self._simple_command('LOGIN', user, self._quote(password)) 597 if typ != 'OK': --> 598 raise self.error(dat[-1]) 599 self.state = 'AUTH' 600 return typ, dat

error: b'[AUTHENTICATIONFAILED] Invalid credentials (Failure)'

Why this error occure? And how do I slove this error? Please tell me.

ricky
  • 11
  • 1
  • 2
  • Have you generated an App Specific Password or enabled "Less Secure Apps"? Google filters out plain logins by default. – Max Nov 11 '19 at 15:30

1 Answers1

1

Are you using 2-FA? if so, you have to set an application-specific password via gmail, and send that password instead of your regular password. Otherwise, this post might help.

It might cause a security hazard - but at least for the development stage you can go to your google account security settings and allow less secure apps (blocked by default).

yo1122
  • 311
  • 4
  • 17
  • 1
    As of May 30 2022, you cannot allow less secure apps https://support.google.com/accounts/answer/6010255?hl=en – jsta Oct 27 '22 at 18:01