Python使用QQ邮箱发送邮件报错smtplib.SMTPAuthenticationError

简介: Python使用QQ邮箱发送邮件报错smtplib.SMTPAuthenticationError

最新在学习Python的基础入门系列课程,今天学习到使用python 的内置库smtplib发送邮件内容。

使用Python发送邮件步骤简单:

  1. 创建SMTP连接
  2. 使用邮箱和密码登录SMTP服务器
  3. 创建邮件内容对象EmailMessage, 并使用set_content方法设置邮件内容
  4. 调用sendmail方法发送邮件

具体代码如下:

import smtplib
from email.message import EmailMessage
# 定义SMTP邮件服务器地址
smtp_server = 'smtp.qq.com'
# 邮件发送人邮箱
from_addr = '******@qq.com'  # 自己的邮想
# 邮件发送人邮箱密码
password = '******'  # 邮箱密码
# 邮件接收人
to_addr = '******@163.com'  # 测试接收邮件地址邮箱
# 创建SMTP连接
conn = smtplib.SMTP_SSL(smtp_server, 465)
# 设计调试级别
conn.set_debuglevel(1)
# 登录邮箱
conn.login(from_addr, password)
# 创建邮件内容对象
msg = EmailMessage()
# 设置邮件内容
msg.set_content('您好,这是一封来自Python的测试邮件', 'plain', 'utf-8')
# 发送邮件
conn.sendmail(from_addr, [to_addr], msg.as_string())
# 退出连接
conn.quit()

运行上述代码后,会报如下错误:

smtplib.SMTPAuthenticationError: (535, b'Error: \xc7\xeb\xca\xb9\xd3\xc3\xca\xda\xc8\xa8\xc2\xeb\xb5\xc7\xc2\xbc\xa1\xa3\xcf\xea\xc7\xe9\xc7\xeb\xbf\xb4: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')

点击网址部分:http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256,会跳转到腾讯邮箱的如下页面微信图片_20220609215528.png就是我们在使用第三方客户端登录QQ邮箱时,为了保证账户安全,需要使用说授权码,在帮助中心中,点击生成授权码就可以得到腾讯邮箱给出的授权码了。

然后,使用得到的授权码,替换下面登录login方法中的password代码,即使用用户邮箱和授权码登录

conn.login(from_addr, "授权码")

修改完成后,重新运行就不会出现以上错误,并且成功的发送邮件到了指定邮箱,可以登录收件邮箱已查看是否收到,也有可能在垃圾箱里。

至此,Python使用smtplib发送邮件成功了。

目录
相关文章
|
6天前
|
开发者 Python
【Python】已解决:(Python3中pip无法安装urllib报错问题) ERROR: Could not find a version that satisfies the requireme
【Python】已解决:(Python3中pip无法安装urllib报错问题) ERROR: Could not find a version that satisfies the requireme
13 0
【Python】已解决:(Python3中pip无法安装urllib报错问题) ERROR: Could not find a version that satisfies the requireme
|
6天前
|
iOS开发 MacOS Python
【Python】已解决:(Pycharm切换Python版本后报错)No Python at “C:\Program Files\Python39\python.exe”
【Python】已解决:(Pycharm切换Python版本后报错)No Python at “C:\Program Files\Python39\python.exe”
11 0
【Python】已解决:(Pycharm切换Python版本后报错)No Python at “C:\Program Files\Python39\python.exe”
|
6天前
|
Python
【Python】已解决:(cmd进入Python环境报错)No Python at ‘C:\Users…\Python\Python39\python.exe’
【Python】已解决:(cmd进入Python环境报错)No Python at ‘C:\Users…\Python\Python39\python.exe’
9 0
|
6天前
|
数据采集 前端开发 测试技术
【Python】已解决:(最新版selenium框架元素定位报错)NameError: name ‘By’ is not defined
【Python】已解决:(最新版selenium框架元素定位报错)NameError: name ‘By’ is not defined
11 0
|
6天前
|
安全 网络安全 Python
【Python】已解决:(pip安装库报错)ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访
【Python】已解决:(pip安装库报错)ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访
13 0
|
6天前
|
Python
【Python】已解决:(Python最新xlrd库读取xlsx报错)SyntaxError: invalid syntax
【Python】已解决:(Python最新xlrd库读取xlsx报错)SyntaxError: invalid syntax
11 0
|
6天前
|
Python
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
【Python】已解决:(pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
13 0
|
6天前
|
开发者 Python
【Python】已解决:(pandas read_excel 读取Excel报错)ImportError: Pandas requires version ‘2.0.1’ or newer of ‘x
【Python】已解决:(pandas read_excel 读取Excel报错)ImportError: Pandas requires version ‘2.0.1’ or newer of ‘x
13 0
|
6天前
|
NoSQL Shell MongoDB
【Python】已解决:(MongoDB安装报错)‘mongo’ 不是内部或外部命令,也不是可运行的程序
【Python】已解决:(MongoDB安装报错)‘mongo’ 不是内部或外部命令,也不是可运行的程序
14 0
|
6天前
|
Python
【Python】已解决:(Python xlwt写入Excel样式报错)ValueError: More than 4094 XFs (styles)
【Python】已解决:(Python xlwt写入Excel样式报错)ValueError: More than 4094 XFs (styles)
9 0