登录凭据不能使用 Gmail SMTP

我正试图通过 Gmail 用 Python 发送一封电子邮件,以下是我的代码:

import smtplib


fromaddr = '......................'
toaddrs  = '......................'
msg = 'Spam email Test'
      

username = '.......'
password = '.......'


server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

我得到了一个错误:

Traceback (most recent call last):
File "email_send.py", line 18, in <module>
server.login(username, password)
File "C:\.....\Python\lib\smtplib.py", line 633
, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepte
d. Learn more at\n5.7.8 http://support.google.com/mail/bin/answer.py?answer=1425
7\n5.7.8 {BADCREDENTIALS} s10sm9426107qam.7 - gsmtp')

这似乎是登录的一个问题。我确信我的登录信息是正确的,除了一件事。用户名应该是“ & quot; brah@gmail.com”,还是简单的“ brah”?我都试过了,一样的错误。

知道是怎么回事吗?

注意: 所有的句点代替了密码/电子邮件/文件路径等。

276662 次浏览

更新 : 这个功能在2022年5月30日之后就不再支持了,请参阅 ref = “ https://Support.google.com/account/answer/6010255? hl = en & amp; access _ id = 637896899107643254-869975220 & amp; p = less-security-apps & amp; rd = 1 # zippy =% 2Cuse-an-app-password”rel = “ norefrer”> https://support.google.com/accounts/answer/6010255?hl=en&visit_id=637896899107643254-869975220&p=less-secure-apps&rd=1#zippy=%2cuse-an-app-password

原答(不再工作了) : 我碰到了一个类似的问题,偶然发现了这个问题。我得到了一个 SMTP 身份验证错误,但我的用户名/通行证是正确的。这就是修好它的方法。我看到这个:

Https://support.google.com/accounts/answer/6010255

简而言之,谷歌不允许你通过 smtplib 登录,因为它标记了这种登录“不太安全”,所以你要做的就是在登录谷歌账户时点击这个链接,并允许访问:

Https://www.google.com/settings/security/lesssecureapps

一旦设置好了(见下面我的截图) ,它就应该可以工作了。

Less Secure Apps

登入可用:

smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login('me@gmail.com', 'me_pass')

更改后的回应:

(235, '2.7.0 Accepted')

事先回应:

smtplib.SMTPAuthenticationError: (535, '5.7.8 Username and Password not accepted. Learn more at\n5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 g66sm2224117qgf.37 - gsmtp')

还是不行吗? 如果您仍然得到 SMTPAuthenticationError,但现在代码是534,这是因为位置是未知的。点击以下连结:

Https://accounts.google.com/displayunlockcaptcha

点击继续,这应该给你10分钟注册你的新应用程序。因此,现在继续进行另一次登录尝试,它应该可以工作。

这似乎不工作的权利,你可能会卡住一段时间,得到这个错误在 smptlib:

235 == 'Authentication successful'
503 == 'Error: already authenticated'

信息说用浏览器登录:

SMTPAuthenticationError: (534, '5.7.9 Please log in with your web browser and then try again. Learn more at\n5.7.9 https://support.google.com/mail/bin/answer.py?answer=78754 qo11sm4014232igb.17 - gsmtp')

启用“ less secureapps”后,去喝杯咖啡,然后回来,再次尝试“ DisplayUnlockCaptcha”链接。从用户体验来看,这种改变可能需要一个小时才能生效。然后再次尝试登录过程。

更新 : : 在这里看到我的回答: 如何使用 Python 作为 Gmail 提供商发送电子邮件?

如果您打开2步验证,您需要生成一个特殊的应用程序密码,而不是使用您的通用密码。 Https://myaccount.google.com/security#signin

我也有同样的问题。身份验证错误可能是由于您的安全设置,例如2步验证。它不允许第三方应用程序覆盖认证。

登录你的谷歌账户,使用以下链接:

第一步〔禁用两步验证连结〕 :

Https://myaccount.google.com/security?utm_source=ogb&utm_medium=act#signin

第二步: [链接允许不太安全的应用程序]

Https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageid=none

现在应该没事了。

我也有同样的问题,所以我在 Mac 上为 Email 应用程序创建了一个应用程序密码来解决这个问题。 你可以在我的帐户-> 安全-> 登录到谷歌-> 应用程序密码。 下面是它的链接。 Https://myaccount.google.com/apppasswords?utm_source=google-account&utm_medium=web

如果出现错误,请参阅(535,b’5.7.8用户名和密码不被接受,详情请参阅 n5.7.8 https://support.google.com/mail/?p=BadCredentials o60sm2132303pje. 21-gsmtp’)

然后只需进入你谷歌帐户设置的安全部分,使一个不太安全的帐户,并打开不太安全的按钮

If you're using 网址: smtp.gmail.com, then you have to do the following:

  1. 打开不太安全的应用程序

  2. 你会收到 gmail 收件箱里的安全邮件,点击“是的,是我”。

  3. 现在再次运行代码。

尝试打开不太安全的应用程序,并确保你有 smtp.gmail.com 或不同的搜索

在我的例子中,我允许访问,但问题是我使用的是 server.login('Name <email>', password)。请务必在这里只使用您的电子邮件: server.login('youremail@gmail.com', password)

更改后的回应:

(235, '2.7.0 Accepted')

事先回应:

smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials 130sm13119106qkh.99 - gsmtp')

我也遇到过同样的问题,我通过允许“不太安全的应用程序访问”解决了这个问题。这可以在谷歌账户的安全标签中找到:enter image description here

拒绝

在 Gmail 中使用 “不太安全的应用程序的访问权限”的解决方案是 拒绝(查找更多的 给你)。

更新

顺便说一下,你可以通过 Google 提出的 < strong > “ App password”解决方案进入 gmail 账户。解决办法很简单:

1. Active two-step verification of the corresponding account.
2. Create an app password.
3. Exactly do the same implementation that you have for sending an email (explained in your question).
Except, replace the password with the generated app password (a sixteen digit password).

更多的细节,你也可以按照 这篇文章(它的工作为我很好)。

当我尝试使用 smtplib时,也会遇到同样的错误消息。错误消息如下所示;

error (535, b'5.7.8 username and password not accepted.

谷歌已经改变了访问不那么安全的应用程序的方式。在那之前,有一个领域可以访问。现在,您可以按照以下步骤操作;

  1. 进入谷歌账户页面 > 安全 > 登录到谷歌部分 打开2步验证。你需要这个功能。 First step

  2. 当2SV 打开时,转到 AppPassword 并为邮件访问生成 new-App-Password。它将为您生成该密码,然后在 Python 中的 gmail_password函数中使用该密码。

    Gmail _ password = ‘ google _ Generator _ it _ for _ you’

Second step

  1. 为你的 Google 账户启用2FA
  2. 然后为你的应用程序设置一个密码
  3. 用那个新密码

对于由 G-Suite 或 Google Workspace 创建的电子邮件,您必须要求管理员激活 Gmail 应用程序并将其分配到您的电子邮件帐户。如果没有这个步骤,尝试上述任何一种方法仍然不起作用。

二零二二年十月三日

根据上面提到的所有答案。 启用2Factor 身份验证并创建 AppPassword 可以实现 SMTP 的功能。

注意: 不太安全的应用程序切换不再工作 tbh? (其他人确认?)

2022年11月更新:

你需要使用 应用程序密码来允许你的应用程序访问你的谷歌帐户。

用 App Passwords 登录:

应用程序密码是一个16位的密码,提供一个不太安全的应用程序或 设备权限访问您的谷歌帐户。应用程序密码可以 只能用于打开了2步验证的帐户。

此外,自2022年5月30日以来,谷歌一直不允许你的应用程序使用 用户名(电子邮件地址)密码访问你的谷歌账户。所以现在,你需要 用户名(电子邮件地址)应用程序密码访问您的谷歌帐户。

不太安全的应用程序和你的谷歌账户 :

为了帮助您的帐户安全,从2022年5月30日起,谷歌号码 更长的支持使用第三方应用程序或设备,要求你 只使用你的用户名和 密码。

如何生成应用程序密码:

首先,从 9个点点击 帐户:

enter image description here

然后,从 保安点击 应用程序密码。 * 在生成 应用程序密码之前不要忘记打开 两步验证 ,否则你不能生成 应用程序密码:

enter image description here

然后,点击 其他 < em > (自定义名称) :

enter image description here

然后,输入你的应用程序名称,然后点击 生成:

enter image description here

最后,您可以生成 应用程序密码 xylnudjdiwpojwzm:

enter image description here

因此,使用 上面的应用程序密码的代码如下所示:

import smtplib


fromaddr = '......................'
toaddrs  = '......................'
msg = 'Spam email Test'


username = '.......'
password = 'xylnudjdiwpojwzm' # Here


server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

此外,settings.py姜戈中的 上面的应用程序密码如下所示:

# "settings.py"


EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'myaccount@gmail.com'
EMAIL_HOST_PASSWORD = 'xylnudjdiwpojwzm' # Here