Python 登录凭据不适用于 Gmail SMTP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16512592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Login credentials not working with Gmail SMTP
提问by Jacob Kudria
I am attempting to send an email in Python, through Gmail. Here is my code:
我正在尝试通过 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()
I get the error:
我收到错误:
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')
This seems to be a problem with the login. I am certain that my login details are correct, except for one thing. Should username be "[email protected]", or simply "blah"? I tried both, same error.
这似乎是登录的问题。我确信我的登录详细信息是正确的,除了一件事。用户名应该是“[email protected]”,还是只是“blah”?我两个都试过,同样的错误。
Any idea whats wrong?
知道出了什么问题吗?
NOTE: all the periods are instead of password/email/file paths/etc.
注意:所有句点都代替密码/电子邮件/文件路径/等。
回答by radtek
I ran into a similar problem and stumbled on this question. I got an SMTP Authentication Error but my user name / pass was correct. Here is what fixed it. I read this:
我遇到了类似的问题并偶然发现了这个问题。我收到 SMTP 身份验证错误,但我的用户名/密码是正确的。这是修复它的方法。我读到这个:
https://support.google.com/accounts/answer/6010255
https://support.google.com/accounts/answer/6010255
In a nutshell, google is not allowing you to log in via smtplib because it has flagged this sort of login as "less secure", so what you have to do is go to this link while you're logged in to your google account, and allow the access:
简而言之,谷歌不允许您通过 smtplib 登录,因为它已将此类登录标记为“不太安全”,因此您必须在登录谷歌帐户时转到此链接,并允许访问:
https://www.google.com/settings/security/lesssecureapps
https://www.google.com/settings/security/lesssecureapps
Once that is set (see my screenshot below), it should work.
一旦设置(见下面我的截图),它应该可以工作。


Login now works:
登录现在有效:
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login('[email protected]', 'me_pass')
Response after change:
更改后的响应:
(235, '2.7.0 Accepted')
Response prior:
之前的回应:
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')
Still not working?If you still get the SMTPAuthenticationError but now the code is 534, its because the location is unknown. Follow this link:
还是行不通?如果您仍然收到 SMTPAuthenticationError 但现在代码是 534,那是因为位置未知。按照这个链接:
https://accounts.google.com/DisplayUnlockCaptcha
https://accounts.google.com/DisplayUnlockCaptcha
Click continue and this should give you 10 minutes for registering your new app. So proceed to doing another login attempt now and it should work.
单击继续,这应该会给您 10 分钟的时间来注册您的新应用程序。因此,现在继续进行另一次登录尝试,它应该可以工作。
This doesn't seem to work right away you may be stuck for a while getting this error in smptlib:
这似乎并没有立即起作用,您可能会遇到以下错误smptlib:
235 == 'Authentication successful'
503 == 'Error: already authenticated'
The message says to use the browser to sign in:
该消息说要使用浏览器登录:
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')
After enabling 'lesssecureapps', go for a coffee, come back, and try the 'DisplayUnlockCaptcha' link again. From user experience, it may take up to an hour for the change to kick in. Then try the sign-in process again.
启用“lesssecureapps”后,去喝杯咖啡,回来,再次尝试“DisplayUnlockCaptcha”链接。根据用户体验,更改最多可能需要一个小时才能生效。然后再次尝试登录过程。
UPDATE:: See my answer here: How to send an email with Gmail as provider using Python?
更新:: 在此处查看我的答案: 如何使用 Python 以 Gmail 作为提供商发送电子邮件?
回答by northcamel
If you turn-on 2-Step Verification, you need generate a special app password instead of using your common password. https://myaccount.google.com/security#signin
如果您开启两步验证,您需要生成一个特殊的应用密码,而不是使用您的常用密码。 https://myaccount.google.com/security#signin
回答by Rahul Shenoy
I had the same issue. The Authentication Error can be because of your security settings, the 2-step verification for instance. It wont allow third party apps to override the authentication.
我遇到过同样的问题。身份验证错误可能是因为您的安全设置,例如两步验证。它不会允许第三方应用程序覆盖身份验证。
Log in to your Google account, and use these links:
登录您的 Google 帐户,并使用以下链接:
Step 1 [Link of Disabling 2-step verification]:
第一步【禁用两步验证链接】:
https://myaccount.google.com/security?utm_source=OGB&utm_medium=act#signin
https://myaccount.google.com/security?utm_source=OGB&utm_medium=act#signin
Step 2: [Link for Allowing less secure apps]
第 2 步:[允许安全性较低的应用程序的链接]
https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageId=none
https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageId=none
It should be all good now.
现在应该一切都好了。
回答by Leo_Liu_MJ
I had same issue. And I fix it with creating an app-password for Email application on Mac. You can find it at my account -> Security -> Signing in to Google -> App passwords. below is the link for it. https://myaccount.google.com/apppasswords?utm_source=google-account&utm_medium=web
我有同样的问题。我通过在 Mac 上为电子邮件应用程序创建应用程序密码来修复它。您可以在我的帐户 -> 安全 -> 登录 Google -> 应用密码中找到它。下面是它的链接。 https://myaccount.google.com/apppasswords?utm_source=google-account&utm_medium=web
回答by vikash tiwary
if you are getting error this(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentialso60sm2132303pje.21 - gsmtp')
如果您收到错误消息(535,b'5.7.8 用户名和密码不被接受。了解更多信息,请访问\n5.7.8 https://support.google.com/mail/?p=BadCredentialso60sm2132303pje.21 - gsmtp')
then simply go in you google accountsettings of security section and make a less secure account and turn on the less secure button
然后只需进入安全部分的谷歌帐户设置并创建一个不太安全的帐户并打开不太安全的按钮
回答by Gaurav
I beleive I'm little late here. But I think this would help for the new peeps! If you're using smtp.gmail.com, then you have to do the following:
我相信我来得有点晚了。但我认为这对新的窥视者有帮助!如果您使用的是smtp.gmail.com,则必须执行以下操作:
Turn on the less secure apps
You'll get the security mail in your gmail inbox, Click Yes,it's me in that.
- Now run your code again.
打开安全性较低的应用程序
你会在你的 gmail 收件箱中收到安全邮件,点击是,里面是我。
- 现在再次运行您的代码。
回答by Azhar Ansari
I had already enabled "Allow less secure apps" and my SMPT python program was working perfectly!
我已经启用了“允许安全性较低的应用程序”并且我的 SMPT python 程序运行良好!
But next day it stared giving me "Bad Credentials error (SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted)". I was using the exact same credentials as before and the less secure apps option was also enabled..!
但第二天它盯着给我“错误的凭据错误(SMTPAuthenticationError:(535,b'5.7.8 用户名和密码不被接受)”。我使用的凭据与以前完全相同,并且还启用了安全性较低的应用程序选项。 !
Changed my password again but that also did not help.
再次更改了我的密码,但这也没有帮助。
Still not working? If you still get the SMTPAuthenticationError but now the code is 534, its because the location is unknown. Follow this link:
还是行不通?如果您仍然收到 SMTPAuthenticationError 但现在代码是 534,那是因为位置未知。按照这个链接:
https://accounts.google.com/DisplayUnlockCaptcha
https://accounts.google.com/DisplayUnlockCaptcha
Click continue and this should give you 10 minutes for registering your new app. So proceed to doing another login attempt now and it should work.
单击继续,这应该会给您 10 分钟的时间来注册您的新应用程序。因此,现在继续进行另一次登录尝试,它应该可以工作。
Note: Had to try multipe times to get this option enabled. Once enabled, I tried connecting after 30mins and it worked..!!!
注意:必须多次尝试才能启用此选项。启用后,我尝试在 30 分钟后连接,并且成功了..!!!
Hope this helps.
希望这可以帮助。

