How to send an email with Gmail as provider using Python?(如何使用 Python 以 Gmail 作为提供商发送电子邮件?)
问题描述
我正在尝试使用 python 发送电子邮件 (Gmail),但出现以下错误.
I am trying to send email (Gmail) using python, but I am getting following error.
Traceback (most recent call last):
File "emailSend.py", line 14, in <module>
server.login(username,password)
File "/usr/lib/python2.5/smtplib.py", line 554, in login
raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.
Python 脚本如下.
The Python script is the following.
import smtplib
fromaddr = 'user_me@gmail.com'
toaddrs = 'user_you@gmail.com'
msg = 'Why,Oh why!'
username = 'user_me@gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
推荐答案
在直接运行 STARTTLS
之前你需要说 EHLO
:
You need to say EHLO
before just running straight into STARTTLS
:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
您还应该真正创建 From:
、To:
和 Subject:
邮件标头,用空行与邮件正文分隔,并且使用 CRLF
作为 EOL 标记.
Also you should really create From:
, To:
and Subject:
message headers, separated from the message body by a blank line and use CRLF
as EOL markers.
例如
msg = "
".join([
"From: user_me@gmail.com",
"To: user_you@gmail.com",
"Subject: Just a message",
"",
"Why, oh why"
])
注意:
为了使其正常工作,您需要启用允许不太安全的应用程序";您的 gmail 帐户配置中的选项.否则,您将收到关键安全警报";当 gmail 检测到非 Google 应用正在尝试登录您的帐户时.
In order for this to work you need to enable "Allow less secure apps" option in your gmail account configuration. Otherwise you will get a "critical security alert" when gmail detects that a non-Google apps is trying to login your account.
这篇关于如何使用 Python 以 Gmail 作为提供商发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Python 以 Gmail 作为提供商发送电子邮件?


基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01