使用 gmail 和 python 发送邮件时出现 SMTPAuthenticationError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26852128/
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
SMTPAuthenticationError when sending mail using gmail and python
提问by mans
when i try to send mail using gmail and python error occurred this type of question are already in this site but doesn't help to me
当我尝试使用 gmail 发送邮件并且发生 python 错误时,此类型的问题已经在此站点中,但对我没有帮助
gmail_user = "[email protected]"
gmail_pwd = "password"
TO = '[email protected]'
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_user,
'Subject: %s' % SUBJECT,
'', TEXT])
server.sendmail(gmail_user, [TO], BODY)
print ('email sent')
error:
错误:
server.login(gmail_user, gmail_pwd)
File "/usr/lib/python3.4/smtplib.py", line 639, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14
<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtl1\n5.7.14 Li2yir27TqbRfvc02CzPqZoCqope_OQbulDzFqL-msIfsxObCTQ7TpWnbxIoAaQoPuL9ge\n5.7.14 BUgbiOqhTEPqJfb02d_L6rrdduHSxv26s_Ztg_JYYavkrqgs85IT1xZYwtbWIRE8OIvQKf\n5.7.14 xxtT7ENlZTS0Xyqnc1u4_MOrBVW8pgyNyeEgKKnKNyxce76JrsdnE1JgSQzr3pr47bL-kC\n5.7.14 XifnWXg> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 fl15sm17237099pdb.92 - gsmtp')
采纳答案by aaveg
Your code looks correct. Try logging in through your browser and if you are able to access your account come back and try your code again. Just make sure that you have typed your username and password correct
您的代码看起来正确。尝试通过浏览器登录,如果您能够访问您的帐户,请返回并再次尝试您的代码。只需确保您输入的用户名和密码正确
EDIT: Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:
编辑:谷歌阻止来自不使用现代安全标准的应用程序的登录尝试(在他们的支持页面上提到)。但是,您可以通过以下链接打开/关闭此安全功能:
Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps
转到此链接并选择打开
https://www.google.com/settings/security/lesssecureapps
回答by ppalacios
I have just sent an email with gmail through Python. Try to use smtplib.SMTP_SSL to make the connection. Also, you may try to change the gmail domain and port.
我刚刚通过 Python 用 gmail 发送了一封电子邮件。尝试使用 smtplib.SMTP_SSL 建立连接。此外,您可以尝试更改 gmail 域和端口。
So, you may get a chance with:
因此,您可能有机会获得:
server = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
server.login(gmail_user, password)
server.sendmail(gmail_user, TO, BODY)
As a plus, you could check the email builtin module. In this way, you can improve the readability of you your code and handle emails headers easily.
另外,您可以检查电子邮件内置模块。通过这种方式,您可以提高代码的可读性并轻松处理电子邮件标题。
回答by taz
Your code looks correct but sometimes google block an ip when you try to send a email since a unusual location, so, you can unblock in the next link
您的代码看起来正确,但有时当您尝试发送电子邮件时,谷歌会阻止一个 ip,因为位置不寻常,因此,您可以在下一个链接中取消阻止
https://support.google.com/accounts/answer/6009563and clicked in accounts.google.com/DisplayUnlockCaptcha .
https://support.google.com/accounts/answer/6009563并点击 accounts.google.com/DisplayUnlockCaptcha 。

