python使用QQ邮箱实现自动发送邮件
作者:技术提高效率 时间:2021-03-03 22:10:06
最近用到Python自动发送邮件,主要就是三步,登录邮件、写邮件内容、发送,用到的库是 smtplib 和 email,直接使用pip安装即可
我使用的是QQ邮箱,首先需要设置QQ邮箱POP3/SMTP服务
记住这个授权码,这个授权码就是Python脚本中登录邮箱时的密码,而不是你平时登录邮箱时的那个密码
一.发送普通文本邮件
#发送多种类型的邮件
from email.mime.multipart import MIMEMultipart
msg_from = '1508691067@qq.com' # 发送方邮箱
passwd = 'xxx' #就是上面的授权码
to= ['1508691067@qq.com'] #接受方邮箱
#设置邮件内容
#MIMEMultipart类可以放任何内容
msg = MIMEMultipart()
conntent="这个是字符串"
#把内容加进去
msg.attach(MIMEText(conntent,'plain','utf-8'))
#设置邮件主题
msg['Subject']="这个是邮件主题"
#发送方信息
msg['From']=msg_from
#开始发送
#通过SSL方式发送,服务器地址和端口
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 登录邮箱
s.login(msg_from, passwd)
#开始发送
s.sendmail(msg_from,to,msg.as_string())
print("邮件发送成功")
二.发送携带附件的邮件
import smtplib
from email.mime.text import MIMEText
#发送多种类型的邮件
from email.mime.multipart import MIMEMultipart
msg_from = '1508691067@qq.com' # 发送方邮箱
passwd = 'xxxxx'
to= ['1508691067@qq.com'] #接受方邮箱
#设置邮件内容
#MIMEMultipart类可以放任何内容
msg = MIMEMultipart()
conntent="这个是字符串"
#把内容加进去
msg.attach(MIMEText(conntent,'plain','utf-8'))
#添加附件
att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #打开附件
att1['Content-Type']='application/octet-stream' #设置类型是流媒体格式
att1['Content-Disposition']='attachment;filename=result.xlsx' #设置描述信息
msg.attach(att1) #加入到邮件中
#设置邮件主题
msg['Subject']="这个是邮件主题"
#发送方信息
msg['From']=msg_from
#开始发送
#通过SSL方式发送,服务器地址和端口
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 登录邮箱
s.login(msg_from, passwd)
#开始发送
s.sendmail(msg_from,to,msg.as_string())
print("邮件发送成功")
三.发送携带图片的附件
同理,可以使用上面的方法也可以发送图片附件
import smtplib
from email.mime.text import MIMEText
#发送多种类型的邮件
from email.mime.multipart import MIMEMultipart
msg_from = '1508691067@qq.com' # 发送方邮箱
passwd = 'xxxxx'
to= ['1508691067@qq.com'] #接受方邮箱
#设置邮件内容
#MIMEMultipart类可以放任何内容
msg = MIMEMultipart()
conntent="这个是字符串"
#把内容加进去
msg.attach(MIMEText(conntent,'plain','utf-8'))
#添加附件
att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #打开附件
att1['Content-Type']='application/octet-stream' #设置类型是流媒体格式
att1['Content-Disposition']='attachment;filename=result.xlsx' #设置描述信息
att2=MIMEText(open('1.jpg','rb').read(),'base64','utf-8')
att2['Content-Type']='application/octet-stream' #设置类型是流媒体格式
att2['Content-Disposition']='attachment;filename=1.jpg' #设置描述信息
msg.attach(att1) #加入到邮件中
msg.attach(att2)
#设置邮件主题
msg['Subject']="这个是邮件主题"
#发送方信息
msg['From']=msg_from
#开始发送
#通过SSL方式发送,服务器地址和端口
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 登录邮箱
s.login(msg_from, passwd)
#开始发送
s.sendmail(msg_from,to,msg.as_string())
print("邮件发送成功")
四.发送 html 格式的邮件
import smtplib
from email.mime.text import MIMEText
#发送多种类型的邮件
from email.mime.multipart import MIMEMultipart
import datetime
msg_from = '1508691067@qq.com' # 发送方邮箱
passwd = 'xxxxxx'
to= ['1508691067@qq.com'] #接受方邮箱
#设置邮件内容
#MIMEMultipart类可以放任何内容
msg = MIMEMultipart()
# conntent="这个是字符串"
# #把内容加进去
# msg.attach(MIMEText(conntent,'plain','utf-8'))
#添加附件
att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #打开附件
att1['Content-Type']='application/octet-stream' #设置类型是流媒体格式
att1['Content-Disposition']='attachment;filename=result.xlsx' #设置描述信息
att2=MIMEText(open('1.jpg','rb').read(),'base64','utf-8')
att2['Content-Type']='application/octet-stream' #设置类型是流媒体格式
att2['Content-Disposition']='attachment;filename=1.jpg' #设置描述信息
msg.attach(att1) #加入到邮件中
msg.attach(att2)
now_time = datetime.datetime.now()
year = now_time.year
month = now_time.month
day = now_time.day
mytime = str(year) + " 年 " + str(month) + " 月 " + str(day) + " 日 "
fayanren="爱因斯坦"
zhuchiren="牛顿"
#构造HTML
content = '''
<html>
<body>
<h1 align="center">这个是标题,xxxx通知</h1>
<p><strong>您好:</strong></p>
<blockquote><p><strong>以下内容是本次会议的纪要,请查收!</strong></p></blockquote>
<blockquote><p><strong>发言人:{fayanren}</strong></p></blockquote>
<blockquote><p><strong>主持人:{zhuchiren}</strong></p></blockquote>
<p align="right">{mytime}</p>
<body>
<html>
'''.format(fayanren=fayanren, zhuchiren=zhuchiren, mytime=mytime)
msg.attach(MIMEText(content,'html','utf-8'))
#设置邮件主题
msg['Subject']="这个是邮件主题"
#发送方信息
msg['From']=msg_from
#开始发送
#通过SSL方式发送,服务器地址和端口
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 登录邮箱
s.login(msg_from, passwd)
#开始发送
s.sendmail(msg_from,to,msg.as_string())
print("邮件发送成功")
来源:https://blog.csdn.net/MATLAB_matlab/article/details/106240424
标签:python,发送邮件
0
投稿
猜你喜欢
Python实现扫码工具的示例代码
2022-07-06 06:06:36
来看看如何防止采集
2007-08-19 20:11:00
Python实现bilibili时间长度查询的示例代码
2022-11-24 15:52:16
浅谈Python 中整型对象的存储问题
2021-08-12 10:33:58
在python中利用dict转json按输入顺序输出内容方式
2021-10-26 15:17:23
浅谈keras中的后端backend及其相关函数(K.prod,K.cast)
2021-07-04 08:53:54
Python3读取UTF-8文件及统计文件行数的方法
2022-06-03 15:02:45
linux安装Python3.4.2的操作方法
2022-06-17 19:19:15
Python实现公历(阳历)转农历(阴历)的方法示例
2021-08-02 09:54:44
python模拟登录并且保持cookie的方法详解
2023-09-06 06:07:16
python可视化爬虫界面之天气查询
2022-09-24 07:34:54
Js中的函数直接量
2007-12-21 19:15:00
Python pytest装饰器总结(实例详解)
2023-06-12 07:15:14
python下setuptools的安装详解及No module named setuptools的解决方法
2022-12-21 00:56:46
一文搞懂Python中is和==的区别
2023-11-15 09:42:27
熵值法原理及Python实现的示例详解
2021-06-19 20:31:33
Python面向对象编程之类的概念
2021-12-24 10:56:33
Oracle建立二进制文件索引的方法
2010-07-18 13:29:00
python数据归一化及三种方法详解
2023-02-28 01:48:40
你的网站使用了微格式了么
2009-05-21 12:10:00