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

查看:64
本文介绍了登录凭据不适用于 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:.....Pythonlibsmtplib.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
5.7.8 http://support.google.com/mail/bin/answer.py?answer=1425
7
5.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

简而言之,谷歌不允许您通过 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
5.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'

提示使用浏览器登录:

SMTPAuthenticationError: (534, '5.7.9 Please log in with your web browser and then try again. Learn more at
5.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天全站免登陆