当我尝试使用gmail发送邮件并发生python错误时,此类问题已经存在于此站点中,但对我没有帮助
gmail_user = "me@gmail.com"
gmail_pwd = "password"
TO = 'friend@gmail.com'
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')
Run Code Online (Sandbox Code Playgroud)
错误:
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')
Run Code Online (Sandbox Code Playgroud)
aav*_*veg 105
您的代码看起来正确.尝试通过浏览器登录,如果您能够访问您的帐户,请再次尝试使用您的代码.只需确保您输入的用户名和密码正确无误
编辑:谷歌阻止来自不使用现代安全标准的应用程序的登录尝试(在其支持页面上提到).但是,您可以通过以下链接打开/关闭此安全功能:
转到此链接,然后选择启用
https://www.google.com/settings/security/lesssecureapps
小智 8
您的代码看起来正确,但由于位置不正常,当您尝试发送电子邮件时,有时google会阻止ip,因此,您可以在下一个链接中取消阻止
https://support.google.com/accounts/answer/6009563并单击account.google.com/DisplayUnlockCaptcha。
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.
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)
Run Code Online (Sandbox Code Playgroud)
另外,您可以检查电子邮件内置模块。通过这种方式,您可以提高代码的可读性并轻松处理电子邮件标题。
| 归档时间: |
|
| 查看次数: |
88119 次 |
| 最近记录: |