从python 3.4脚本发送电子邮件,没有启用不太安全的应用程序'在gmail中

时间:2015-04-13 16:05:21

标签: gmail smtplib

我想从我的Gmail地址使用python 3.4脚本发送邮件。 我使用以下代码:

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帐户中,脚本运行正常。但是,如果我停用不太安全的应用程序,它就不起作用(我收到来自谷歌的警告电子邮件,其中“登录尝试已被阻止”)。我想修改我的代码,以便能够在不启用此功能的情况下发送邮件。

我已经阅读了有关类似问题的所有问题和答案,但没有找到任何有用的答案或方法。有人对此有任何解决方案吗?

1 个答案:

答案 0 :(得分:2)

来自"Allowing less secure apps to access your account" support page

  

Google可能阻止某些不使用现代安全标准的应用或设备尝试登录。

登录/密码不是验证的现代机制。您应该实施SASL XOAuth2