登录凭据不适用于Gmail SMTP [英] Login credentials not working with Gmail SMTP

查看:291
本文介绍了登录凭据不适用于Gmail SMTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Gmail通过Python发送电子邮件.这是我的代码:

I am attempting to send an email in Python, through Gmail. Here is my code:

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')

这似乎是登录问题.我确信我的登录详细信息是正确的,除了一件事.用户名应该是"blah@gmail.com",还是仅仅是"blah"?我都尝试了,同样的错误.

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 "blah@gmail.com", or simply "blah"? I tried both, same error.

有什么想法吗?

注意:所有时间段都代替了密码/电子邮件/文件路径/等.

NOTE: all the periods are instead of password/email/file paths/etc.

推荐答案

我遇到了类似的问题,偶然发现了这个问题.我收到SMTP身份验证错误,但我的用户名/密码正确.这是修复它的原因.我读到了:

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:

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

简而言之,Google不允许您通过smtplib登录,因为它已将这种登录标记为安全性较低",因此您要做的就是在登录自己的网站时转到此链接. google帐户,并允许访问:

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:

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

设置完成后(请参见下面的屏幕截图),它应该可以工作.

Once that is set (see my screenshot below), it should work.

现在可以登录:

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,则因为位置未知.点击此链接:

Still not working? If you still get the SMTPAuthenticationError but now the code is 534, its because the location is unknown. Follow this link:

https://accounts.google.com/DisplayUnlockCaptcha

单击继续,这将给您10分钟的时间注册新应用.因此,请立即进行另一次登录尝试,它应该可以正常工作.

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.

这似乎无法立即生效,您可能会在smptlib中收到此错误一段时间:

This doesn't seem to work right away you may be stuck for a while getting this error in 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')

启用"lesssecureapps"后,去喝咖啡,回来,然后再次尝试"DisplayUnlockCaptcha"链接.根据用户体验,更改最多可能需要一个小时才能生效.然后再次尝试登录过程.

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.

更新 ::在这里查看我的答案: 如何通过以下方式发送电子邮件Gmail是使用Python的提供商吗?

UPDATE:: See my answer here: How to send an email with Gmail as provider using Python?

这篇关于登录凭据不适用于Gmail SMTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆