gmail login failure using python and imaplib

With the new gmail´s update, some mail servr or apps get blocked due to the new gmails security settings. To solve this, I went to https://www.google.com/settings/security page and 'enabled' Access for less secure apps.


You can try to turn on this: https://www.google.com/settings/security/lesssecureapps This action solved the same problem for me.


There are mainly 3 reasons why this error occurs:

  1. Internet Connection Issue
  2. Incorrect Login Credentials
  3. myaccount.google.com/lesssecureapps is turned off

If you want to avoid this error without compromising your account's security, use OAuth to authenticate. The protocol is documented here, and there is Python sample code that shows the use of XOAUTH2 with imaplib.

Independent of this, you should consider enabling two-step verification on your account to make it more secure. If you do, you can use an App Password to connect to IMAP, which might also avoid the above warning.