从 python 3.4 脚本发送电子邮件,而不在 gmail 中启用“不太安全的应用程序" [英] sending email from python 3.4 script, WITHOUT enabling 'less secure apps' in gmail
问题描述
我想使用 python 3.4 脚本从我的 gmail 地址发送邮件.我使用以下代码:
I would like to send mail using a python 3.4 script, from my gmail address. I use the following code:
import smtplib
def sendmail():
sender='myemail@gmail.com'
receiver=['someone@gmail.com']
message='text here'
try:
session=smtplib.SMTP('smtp.gmail.com',587)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender,'mypassword')
session.sendmail(sender,receiver,message)
session.quit()
except smtplib.SMTPException:
print('Error, can not send mail!')
如果我在我的 gmail 帐户中允许安全性较低的应用程序",则该脚本可以正常工作.但是,如果我禁用不太安全的应用程序",它就不起作用(我收到来自谷歌的警告电子邮件,其中登录尝试被阻止").我想修改我的代码,以便能够在不启用此功能的情况下发送邮件.
If I 'allow less secure apps' in my gmail account, the script works fine. However, if I disable 'less secure apps', it doesn't work (I get a warning email from google, with 'sign-in attempt blocked') . I would like to modify my code, to be able to send mail without enabling this thing.
我已阅读有关类似问题的所有问题和答案,但没有找到任何有用的答案或方法.有人对此有解决方案吗?
I have read all the questions and answers regarding similar problems, but did not find any useful answers or methods. Does someone have a solution for this?
推荐答案
Google 可能会阻止来自不使用现代安全标准的某些应用或设备的登录尝试.
Google may block sign in attempts from some apps or devices that do not use modern security standards.
登录名/密码不是现代的身份验证机制.您应该实施 SASL XOAuth2.
A login/password is not a modern mechanism to authenticate. You should implement SASL XOAuth2.
这篇关于从 python 3.4 脚本发送电子邮件,而不在 gmail 中启用“不太安全的应用程序"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!