Jac*_*ria 27 python smtp login smtp-auth
我正在尝试通过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()
Run Code Online (Sandbox Code Playgroud)
我收到错误:
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')
Run Code Online (Sandbox Code Playgroud)
这似乎是登录的问题.我确信我的登录详细信息是正确的,除了一件事.用户名应该是"blah@gmail.com",还是只是"等等"?我试过两个,同样的错误.
有什么想法错了吗?
注意:所有句点都代替密码/电子邮件/文件路径/等.
rad*_*tek 68
我遇到了类似的问题,偶然发现了这个问题.我收到了SMTP身份验证错误,但我的用户名/密码是正确的.这是修复它的原因.我看了这个:
https://support.google.com/accounts/answer/6010255
简而言之,Google不允许您通过smtplib登录,因为它已将此类登录标记为"不太安全",因此您需要做的是在登录Google帐户时转到此链接,并允许访问:
https://www.google.com/settings/security/lesssecureapps
设置完成后(请参阅下面的屏幕截图),它应该可以正常工作.

现在登录有效:
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login('me@gmail.com', 'me_pass')
Run Code Online (Sandbox Code Playgroud)
更改后的回复:
(235, '2.7.0 Accepted')
Run Code Online (Sandbox Code Playgroud)
事先回复:
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')
Run Code Online (Sandbox Code Playgroud)
还是行不通?如果你仍然得到SMTPAuthenticationError,但现在代码是534,因为它的位置是未知的.点击此链接:
https://accounts.google.com/DisplayUnlockCaptcha
单击"继续",这将为您提供10分钟的注册新应用程序.所以现在继续进行另一次登录尝试,它应该工作.
这似乎不会马上工作你可能会被困在一段时间得到这个错误smptlib:
235 == 'Authentication successful'
503 == 'Error: already authenticated'
Run Code Online (Sandbox Code Playgroud)
消息说使用浏览器登录:
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')
Run Code Online (Sandbox Code Playgroud)
启用'lesssecureapps'后,去喝咖啡,回来,再次尝试'DisplayUnlockCaptcha'链接.根据用户体验,更改可能需要一个小时才能启动.然后再次尝试登录过程.
更新 ::请在此处查看我的答案: 如何使用Python以Gmail作为提供商发送电子邮件?
小智 64
当我尝试使用 时,我也遇到相同的错误消息smtplib。错误信息是这样的;
error (535, b'5.7.8 username and password not accepted.
Run Code Online (Sandbox Code Playgroud)
谷歌已经改变了对不太安全的应用程序的访问权限。在此之前,有一个字段可供访问。现在您可以按照以下步骤操作;
步骤略有改变
生成用于邮件访问的新应用程序密码。它会为您生成它,然后使用该密码gmail_passwordPython 的函数中使用这个密码。
gmail_password='google_generate_it_for_you'
OmG*_*OmG 15
在 Gmail 中使用“访问不太安全的应用程序”的解决方案已被拒绝(在此处查找更多信息)。
顺便说一句,您可以通过Google提出的解决方案访问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).
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,您也可以关注这篇文章(它对我来说效果很好)。
小智 14
我遇到过同样的问题。身份验证错误可能是由于您的安全设置所致,例如两步验证。它不会允许第三方应用覆盖身份验证。
步骤1 [禁用两步验证的链接]:
https://myaccount.google.com/security?utm_source=OGB&utm_medium=act#signin
步骤2:[允许不安全的应用程序链接]
https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageId=none
现在应该一切都好。
小智 9
我有同样的问题。我通过在 Mac 上为电子邮件应用程序创建应用程序密码来修复它。您可以在我的帐户 -> 安全 -> 登录 Google -> 应用密码中找到它。下面是它的链接。 https://myaccount.google.com/apppasswords?utm_source=google-account&utm_medium=web
小智 5
如果您收到错误消息(535,b'5.7.8 用户名和密码不被接受。了解更多信息,请访问\n5.7.8 https://support.google.com/mail/?p=BadCredentials o60sm2132303pje.21 - gsmtp')
然后只需进入安全部分的谷歌帐户设置并创建一个不太安全的帐户并打开不太安全的按钮