I encounter an AUTHENTICATIONFAILED error when connecting to email with imaplib library

谁说胖子不能爱 提交于 2021-02-17 02:11:06

问题


How can I connect to imaplib library without encountering AUTHENTICATIONFAILE error !?

My Gmail inbox shows me a serious security alert (login attempt blocked) when logging in through the web browser.

IMAP_SERVER = 'imap.gmail.com'
USERNAME = '******@gmail.com'
PASSWORD = '******'

client = imaplib.IMAP4_SSL(IMAP_SERVER)
client.login(USERNAME, PASSWORD)

回答1:


This is most likely gmail blocking "less secure apps" from connecting to your account. This blocking is enabled, by default.

From a google support page:

You can block sign-in attempts from some apps or devices that are less secure. Apps that are less secure don't use modern security standards, such as OAuth. Using apps and devices that don’t use modern security standards increases the risk of accounts being compromised. Blocking these apps and devices helps keep your users and data safe.

If you would like to turn on access to less secure apps (which will make your account more vulnerable to being attacked), then you can follow these steps:

  1. Sign into your google account normally. (https://myaccount.google.com/)

  2. Once signed in, go to the "Security" page.

  3. Scroll down until you see a window with "Less secure app access". If you want to access your email using python code like you posted, this needs to be turned on.

Personally, I think this is acceptable on a throwaway/testing account but I would never do this to my main/personal account or any account involving communicating with clients.

In fact, take a look at this page: https://developers.google.com/gmail/api/quickstart/python Gmail has a dedicated API for connecting to your google mail account. Their api implements proper security measures which means you won't need to enable less secure app access... protecting your account!



来源:https://stackoverflow.com/questions/57963552/i-encounter-an-authenticationfailed-error-when-connecting-to-email-with-imaplib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!