如何使用python通过gmail发送电子邮件? [英] How to send an email through gmail using python?

查看:73
本文介绍了如何使用python通过gmail发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 使用我的 gmail 帐户发送电子邮件.我已经在这里和互联网上阅读了很多问题,但没有一个能解决我的问题.

I'm trying to send emails with my gmail account using python. I've already read many questions here and around the Internet, but none of them solved my problem.

我使用的代码如下(感谢 rosettacode),这是与可以找到的有关此主题的许多其他代码片段非常相似:

The code that I'm using is the following (thanks to rosettacode), which is very similar to many other code snippets that can be found about this topic:

def sendemail(from_addr, to_addr_list, cc_addr_list,
              subject, message,
              login, password,
              smtpserver='smtp.gmail.com:587'):
    header  = 'From: %s\n' % from_addr
    header += 'To: %s\n' % ','.join(to_addr_list)
    header += 'Cc: %s\n' % ','.join(cc_addr_list)
    header += 'Subject: %s\n\n' % subject
    message = header + message

    server = smtplib.SMTP(smtpserver)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(login,password)
    problems = server.sendmail(from_addr, to_addr_list, message)
    server.quit()
    return problems

我的问题是在登录阶段.它返回以下错误消息:

My problem is during the login phase. It returns the following error message:

SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsMX\n5.7.14 Z4_8qLgwTbhS2CwFvVApFvRfpIS1Vbbfun6gHcf0D6jgSQ-ixMn79mf3AivveTs9IhYsgq\n5.7.14 pmrp157H4Vmk6-ybAC9u2d2lNMYyy5pdmociqeSxBBwFGEPGJKHKdJpSocx86gzG-im6V-\n5.7.14 hsOeMKiJRAuGZjHUprEwj8oABwLzWQ8vEzovpXk79M-i8cnFseW-PNLxLlsK21WaLHLKmZ\n5.7.14 Ll3tEgQ> 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 dc8sm25406976wib.7 - gsmtp')

我按照建议的链接找到了这个答案,但我没有知道它是否可以成为解决方案.

I followed the suggested link and I found this answer, but I don't know if it could be the solution.

那么,有什么问题吗?我的账户设置?我的代码?

So, what's the problem? My account's settings? My code?

推荐答案

Google 最近加强了安全性.直接使用用户名/密码的应用程序已被停用.所有用户仍然可以在他们的安全设置中重新激活这些安全性较低的应用程序,因为您已经阅读了您在问题中提供的链接.这是目前唯一的解决方案.

Google has recently tightened their security. Application that use username/password directly have been deactivated. All users are still able to reactivate these less secure application in their security settings as you have been reading in the link you gave in your question. This is the only solution at this point.

另一种方法是使用其他 SMTP 服务器进行发送.

The alternative would be to use an other SMTP server for sending.

这篇关于如何使用python通过gmail发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆