sending email from python 3.4 script, WITHOUT enabling 'less secure apps' in gmail

怎甘沉沦 提交于 2021-02-04 16:46:34

问题


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!')

if i 'allow less secure apps' in my gmail account, the script works fine. however, if i disable 'less secure apps', it doesn't works (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. someone has any solution for this?


回答1:


From "Allowing less secure apps to access your account" support page

Google may block sign in attempts from some apps or devices that do not use modern security standards.

A login/password is not a modern mechanism to authenticate. You should implement SASL XOAuth2.



来源:https://stackoverflow.com/questions/29609960/sending-email-from-python-3-4-script-without-enabling-less-secure-apps-in-gma

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!