我正在尝试通过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')
这似乎是登录的问题。我确信我的登录详细信息是正确的,除了一件事。用户名应该是“[email protected]”,还是只是“等等”?我试过两个,同样的错误。
有什么想法错了吗?
注意:所有句点都代替密码/电子邮件/文件路径/等。
我遇到了类似的问题,偶然发现了这个问题。我收到了SMTP身份验证错误,但我的用户名/密码是正确的。这是修复它的原因。我看了这个:
https://support.google.com/accounts/answer/6010255
简而言之,谷歌不允许您通过smtplib登录,因为它已将此类登录标记为“不太安全”,因此您需要做的是在登录Google帐户时转到此链接,并允许访问:
https://www.google.com/settings/security/lesssecureapps
设置完成后(请参阅下面的屏幕截图),它应该可以正常工作。
现在登录有效:
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login('[email protected]', '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')
启用'lesssecureapps'后,去喝咖啡,回来,再次尝试'DisplayUnlockCaptcha'链接。根据用户体验,更改可能需要一个小时才能启动。然后再次尝试登录过程。
更新::请参阅我的回答:How to send an email with Gmail as provider using Python?
我遇到过同样的问题。身份验证错误可能是因为您的安全设置,例如两步验证。它不允许第三方应用程序覆盖身份验证。
第1步[禁用2步验证的链接]:
https://myaccount.google.com/security?utm_source=OGB&utm_medium=act#signin
第2步:[链接以允许安全性较低的应用]
https://myaccount.google.com/u/1/lesssecureapps?pli=1&pageId=none
现在应该都很好。
如果您启用了两步验证,则需要生成一个特殊的应用密码,而不是使用您的通用密码。 https://myaccount.google.com/security#signin
我有同样的问题。我通过在Mac上为电子邮件应用程序创建应用密码来解决此问题。您可以在我的帐户中找到它 - >安全性 - >登录Google - >应用程序密码。下面是它的链接。 https://myaccount.google.com/apppasswords?utm_source=google-account&utm_medium=web