python使用QQ邮箱实现自动发送邮件


Posted in Python onJune 22, 2020

最近用到Python自动发送邮件,主要就是三步,登录邮件、写邮件内容、发送,用到的库是 smtplib 和 email,直接使用pip安装即可

我使用的是QQ邮箱,首先需要设置QQ邮箱POP3/SMTP服务

python使用QQ邮箱实现自动发送邮件

python使用QQ邮箱实现自动发送邮件

python使用QQ邮箱实现自动发送邮件

记住这个授权码,这个授权码就是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("邮件发送成功")

python使用QQ邮箱实现自动发送邮件

二.发送携带附件的邮件

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("邮件发送成功")

python使用QQ邮箱实现自动发送邮件

三.发送携带图片的附件

同理,可以使用上面的方法也可以发送图片附件

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("邮件发送成功")

python使用QQ邮箱实现自动发送邮件

四.发送 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("邮件发送成功")

python使用QQ邮箱实现自动发送邮件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python 字符串操作实现代码(截取/替换/查找/分割)
Jun 08 Python
wxPython窗口中文乱码解决方法
Oct 11 Python
17个Python小技巧分享
Jan 23 Python
python爬取足球直播吧五大联赛积分榜
Jun 13 Python
解决项目pycharm能运行,在终端却无法运行的问题
Jan 19 Python
详解Python中pandas的安装操作说明(傻瓜版)
Apr 08 Python
window7下的python2.7版本和python3.5版本的opencv-python安装过程
Oct 24 Python
python实现超市商品销售管理系统
Oct 25 Python
用python中的matplotlib绘制方程图像代码
Nov 21 Python
python颜色随机生成器的实例代码
Jan 10 Python
基于python代码批量处理图片resize
Jun 04 Python
用于ETL的Python数据转换工具详解
Jul 21 Python
浅谈keras中loss与val_loss的关系
Jun 22 #Python
python实现简易版学生成绩管理系统
Jun 22 #Python
python能否java成为主流语言吗
Jun 22 #Python
python让函数不返回结果的方法
Jun 22 #Python
python实现学生成绩测评系统
Jun 22 #Python
python算的上脚本语言吗
Jun 22 #Python
Python读取二进制文件代码方法解析
Jun 22 #Python
You might like
新手学PHP之数据库操作详解及乱码解决!
2007/01/02 PHP
php简单封装了一些常用JS操作
2007/02/25 PHP
PHP 字符串正则替换函数preg_replace使用说明
2011/07/15 PHP
ThinkPHP3.1.3版本新特性概述
2014/06/19 PHP
11个PHPer必须要了解的编程规范
2014/09/22 PHP
php微信公众开发之获取周边酒店信息的方法
2014/12/22 PHP
php通过Chianz.com获取IP地址与地区的方法
2015/01/14 PHP
php中使用sftp教程
2015/03/30 PHP
JavaScript 新手24条实用建议[TUTS+]
2009/06/21 Javascript
Jquery之美中不足小结
2011/02/16 Javascript
jQuery EasyUI API 中文文档 - NumberBox数字框
2011/10/13 Javascript
jQuery EasyUI API 中文文档 - Spinner微调器使用
2011/10/21 Javascript
在服务端(Page.Write)调用自定义的JS方法详解
2013/08/09 Javascript
FF(火狐)浏览器无法执行window.close()解决方案
2014/11/13 Javascript
jquery表单验证需要做些什么
2015/11/17 Javascript
js实现纯前端的图片预览
2016/04/27 Javascript
用React-Native+Mobx做一个迷你水果商城APP(附源码)
2017/12/25 Javascript
Django2.1集成xadmin管理后台所遇到的错误集锦(填坑)
2018/12/20 Python
python3实现斐波那契数列(4种方法)
2019/07/15 Python
python傅里叶变换FFT绘制频谱图
2019/07/19 Python
python os.fork() 循环输出方法
2019/08/08 Python
对pytorch中的梯度更新方法详解
2019/08/20 Python
使用python动态生成波形曲线的实现
2019/12/04 Python
python 用opencv实现图像修复和图像金字塔
2020/11/27 Python
python利用xpath爬取网上数据并存储到django模型中
2021/02/26 Python
html5嵌入内容_动力节点Java学院整理
2017/07/07 HTML / CSS
HTML5实现的震撼3D焦点图动画的示例代码
2019/09/26 HTML / CSS
新西兰购物网站:TheMarket NZ
2020/09/19 全球购物
长曲棍球装备:Lacrosse Monkey
2020/12/02 全球购物
几个SQL的面试题
2014/03/08 面试题
领导视察欢迎词
2014/01/15 职场文书
《植物妈妈有办法》教学反思
2014/02/25 职场文书
公司的门卫岗位职责
2014/09/09 职场文书
出租车拒载检讨书
2015/01/28 职场文书
小学教师师德师风自我评价
2015/03/04 职场文书
创业计划书之甜品店
2019/09/18 职场文书