Python基于QQ邮箱实现SSL发送
作者:天天向上327 时间:2021-07-29 10:35:30
一、QQ邮箱SSL发送
获取qq授权码
ssl发送方式不是使用邮箱密码,而是需要授权码,具体步骤如下:
登录发送人qq邮箱>>设置>>账户>>POP3/STMP服务开启>>生成授权码
验证密保
复制16位授权码
qq邮箱发送源码
#!/usr/bin/python3
# encoding:utf-8
'''
Created on 2020-04-24 12:15
@author: Administrator
'''
#coding:utf-8
import smtplib
from email.mime.text import MIMEText # 引入smtplib和MIMEText
from email.mime.multipart import MIMEMultipart
#设置SMTP地址
host = 'smtp.qq.com'
#设置发件服务器端口号,注意,这里有SSL和非SSL两种形式,qq SSL端口为465,非SSL为端口默认25
port = "465"
#设置发件邮箱
sender = "357@qq.com"
#设置发件邮箱的授权码 ,qq邮箱ssl发送需要先开启stmp并获取密码
pwd = 'sqmqweertyuiioplk' #16授权码
#设置邮件接收人,发送给多人,隔开
receiver = 'yiwr@163.com,7894@qq.com'
#设置邮件抄送人,发送给多人,隔开
cc = 'ywr198592@126.com'
''' 不带附件发送邮件
#设置html格式的邮件
#body = '<h1>这是一个python测试邮件</h1><p>test</p>'
#msg = MIMEText(body, 'html') # 设置正文为符合邮件格式的HTML内容
#发送普通格式邮件
msg = MIMEText('Python 普通格式,邮件发送测试...', 'plain', 'utf-8')
'''
#需要发送附件的方法实例
msg = MIMEMultipart()
#设置发送头信息
msg.add_header('subject', '测试邮件') #设置邮件标题
msg.add_header('from', sender) # 设置发送人
msg.add_header('to', receiver) # 设置接收人
msg.add_header('Cc',cc) # 抄送人
#设置正文内容
msg.attach(MIMEText('Python 邮件发送测试...', 'plain', 'utf-8'))
#设置附件1,D://cs.txt 文件
att1 = MIMEText(open('D://cs.txt', 'rb').read(), 'base64', 'utf-8')
att1.add_header('Content-Type', 'application/octet-stream')
# 这里的filename可以任意写,写什么名字,邮件中显示附件的名字
att1.add_header('Content-Disposition', 'attachment', filename='cs.txt')
msg.attach(att1)
try:
#注意!如果是使用非SSL端口,这里就要改为SMTP
smtpObj = smtplib.SMTP_SSL(host, port)
#登陆邮箱
smtpObj.login(sender, pwd)
#发送邮件,注意第二个参数是发送人抄送人地址
smtpObj.sendmail(sender, receiver.split(',') + cc.split(','), msg.as_string())
print ("发送成功")
except smtplib.SMTPException as e:
print ("发送失败")
print(e)
finally:
smtpObj.quit()
发送之后结果截图
二、163邮箱非SSL发送
非ssl无需获取授权码,直接配置邮箱密码即可
163邮箱发送源码
#!/usr/bin/python3
#encoding:utf-8
'''
Created on 2020-04-24 12:15
@author: Administrator
'''
#coding:utf-8
import smtplib
from email.mime.text import MIMEText #引入smtplib和MIMEText
from email.mime.multipart import MIMEMultipart
#设置SMTP地址
host = 'smtp.163.com'
#设置发件服务器端口号。注意,这里有SSL和非SSL两种形式,非SSL默认端口25
port = 25
#设置发件邮箱
sender = "yiwr@163.com"
#设置发件邮箱密码
pwd = 'xxxx'
#设置邮件接收人,发送给多人,隔开
receiver = '7894@qq.com'
#设置邮件抄送人,发送给多人,隔开
cc = '357@qq.com'
''' 不带附件发送邮件
#设置html格式的邮件
#body = '<h1>这是一个python测试邮件</h1><p>test</p>'
#msg = MIMEText(body, 'html') #设置正文为符合邮件格式的HTML内容
#发送普通格式邮件
msg = MIMEText('Python 普通格式,邮件发送测试...', 'plain', 'utf-8')
'''
#附件方法实例
msg = MIMEMultipart()
#设置头信息
msg.add_header('subject', '测试邮件') #设置邮件标题
msg.add_header('from', sender) #设置发送人
msg.add_header('to', receiver) #设置接收人
msg.add_header('Cc',cc) # 抄送人
#设置正文内容
msg.attach(MIMEText('Python 邮件发送测试...', 'plain', 'utf-8'))
#设置附件1,D://cs.txt 文件
att1 = MIMEText(open('D://cs.txt', 'rb').read(), 'base64', 'utf-8')
att1.add_header('Content-Type', 'application/octet-stream')
#这里的filename可以任意写,写什么名字,邮件中显示附件的名字
att1.add_header('Content-Disposition', 'attachment', filename='cs.txt')
msg.attach(att1)
try:
#注意!如果是使用SSL端口,这里就要改为SMTP_SSL
smtpObj = smtplib.SMTP(host, port)
#登陆邮箱
smtpObj.login(sender, pwd)
#发送邮件,注意第二个参数是发送人抄送人地址
smtpObj.sendmail(sender, receiver.split(',') + cc.split(','), msg.as_string())
print ("发送成功")
except smtplib.SMTPException as e:
print ("发送失败")
print(e)
finally:
smtpObj.quit()
发送之后结果截图
三、问题
3.1 python通过qq邮箱,SMTP发送邮件失败:
问题描述:使用qq账户及密码SSL方式发送邮件,报错:(535, b'Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')
解决方案:开启POP3/SMTP服务,获取授权码,qq源码的邮箱密码改成授权码即可
3.2 html附件变.bin文件后缀
问题描述:发送一个html格式的附件,收到邮件发送后缀变成.bin的文件,如图:
解决方案:把 att1["Content-Disposition"] = 'attachment; filename="' + "接口测试报告.html" 改为 att1.add_header('Content-Disposition', 'attachment', filename='接口测试报告.html')
来源:https://www.cnblogs.com/yiwenrong/p/12771554.html
标签:Python,QQ,邮箱,SSL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python入门教程4. 元组基本操作 <font color=red>原创</font>
2021-01-12 09:53:21
Python3 Tkinkter + SQLite实现登录和注册界面
2023-10-01 14:56:15
![](https://img.aspxhome.com/file/2023/5/62945_0s.jpg)
PHP实现生成Excel文件并导出的示例详解
2023-05-28 13:11:18
mysql分表的3种方法
2011-01-29 16:50:00
使用python把xmind转换成excel测试用例的实现代码
2022-12-21 06:21:34
![](https://img.aspxhome.com/file/2023/6/68846_0s.png)
连续字符自动换行的解决方案
2008-12-04 16:39:00
Dreamweaver的CSS布局ul和li范例
2009-08-28 12:34:00
![](https://img.aspxhome.com/file/UploadPic/20098/28/2q02543h09_lit-69s.gif)
Windows Server 2008之数据安全保护
2008-12-04 12:49:00
如何制作关联的下拉菜单?
2010-06-29 21:19:00
MySQL错误中文参照列表
2010-09-30 14:41:00
100%点击区的滑动门
2007-06-14 22:00:00
python虚拟环境迁移方法
2021-09-13 07:52:51
![](https://img.aspxhome.com/file/2023/0/69000_0s.jpg)
微软SQLServer密码管理的危险判断
2008-11-24 17:34:00
Dreamweaver量身打造Wordpress留言板(三)
2009-12-13 18:45:00
![](https://img.aspxhome.com/file/UploadPic/200912/13/1444290-13s.gif)
Python对XML文件实现增删改查操作
2023-11-19 20:42:03
网页超级链接加上快捷键方法
2010-03-16 12:28:00
Python使用Asyncio进行web编程方法详解
2022-08-11 17:03:56
![](https://img.aspxhome.com/file/2023/8/70798_0s.png)
富文本编辑器的基本原理与实践
2008-06-13 13:28:00
VB应用程序访问SQL Server的常用方法
2009-01-21 14:28:00
ASP+JAVAScript:复杂表单的动态生成与验证
2007-10-06 21:51:00