将Gmail与Python 2.7一起使用的问题

时间:2017-05-01 17:27:00

标签: python-2.7 gmail

这里有新人...... 我在这个网站上搜索并尝试了我的代码的多种变体,但是当我尝试使用Python 2.7通过Gmail发送电子邮件时,我仍然收到登录错误。我在Gmail帐户中启用了“安全性较低的应用”功能,但我仍然收到此错误:

追踪(最近一次通话):   文件“T:\ OC \ Projects \ Aquadat \ Scripting \ RawArcPyScripts \ sendEmailWithAttachment_Aquadat.py”,第28行,     svr.login(发件人,PWD)   登录时,文件“C:\ Python27 \ ArcGIS10.3 \ lib \ smtplib.py”,第615行     提出SMTPAuthenticationError(代码,resp) SMTPAuthenticationError:(534,'5.7.14请通过您的网络浏览器和\ n5.7.14登录,然后再试一次。\ n5.7.14了解更多信息,请访问\ n5.7.14 https://support.google.com/mail/answer/78754 z33sm11383168qta.48 - gsmtp')

这是有问题的代码。我从这个网站上抓了很多,并试图让它适应我的应用程序。任何帮助都将非常感激。

import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate

svr = smtplib.SMTP_SSL('smtp.gmail.com',465)
svr.ehlo()
svr.starttls()
svr.ehlo()
sender = 'my_name'
pwd = 'my_pwd'
svr.login(sender,pwd)
rcvr = sender #Change to arcpy.GetParameterAsText(x) when working
def send_mail(send_from,send_to,subject,text,files,
              server):
    assert isinstance(send_to, list)

    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach(MIMEText(text))

    for f in files or []:
        with open(f, "rb") as fil:
            part = MIMEApplication(
                fil.read(),
                Name=basename(f)
            )
            part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
            msg.attach(part)


        smtp = smtplib.SMTP_SSL(server)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()

0 个答案:

没有答案
相关问题