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 第一步 hello world
Sep 25 Python
详解Python中的条件判断语句
May 14 Python
Python中的浮点数原理与运算分析
Oct 12 Python
python生成圆形图片的方法
Mar 25 Python
django+mysql的使用示例
Nov 23 Python
pytorch 中pad函数toch.nn.functional.pad()的用法
Jan 08 Python
pytorch实现mnist分类的示例讲解
Jan 10 Python
Python更换pip源方法过程解析
May 19 Python
Python pip install之SSL异常处理操作
Sep 03 Python
Python控制鼠标键盘代码实例
Dec 08 Python
Python中zipfile压缩包模块的使用
May 14 Python
使用Python拟合函数曲线
Apr 14 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
FireFox浏览器使用Javascript上传大文件
2013/10/30 PHP
PHP删除数组中空值的方法介绍
2014/04/14 PHP
PHP中递归的实现实例详解
2017/11/14 PHP
javascript中的location用法简单介绍
2007/03/07 Javascript
用javascript自动显示最后更新时间
2007/03/15 Javascript
jQuery阻止同类型事件小结
2013/04/19 Javascript
js数组去重的常用方法总结
2014/01/24 Javascript
js中document.write使用过程中的一点疑问解答
2014/03/20 Javascript
JavaScript动态添加列的方法
2015/03/25 Javascript
浅谈js停止事件冒泡 阻止浏览器的默认行为(阻止超连接 #)
2017/02/08 Javascript
利用js判断手机是否安装某个app的多种方案
2017/02/13 Javascript
Vue.js实现网格列表布局转换方法
2017/08/25 Javascript
vue-cli webpack 引入jquery的方法
2018/01/10 jQuery
微信小程序网络请求封装示例
2018/07/24 Javascript
使用Angular 6创建各种动画效果的方法
2018/10/10 Javascript
Javascript地址引用代码实例解析
2020/02/25 Javascript
基于javascript处理nginx请求过程详解
2020/07/07 Javascript
Vue的自定义组件不能使用click方法的解决
2020/07/28 Javascript
JS删除对象中某一属性案例详解
2020/09/08 Javascript
js重写alert事件(避免alert弹框标题出现网址)
2020/12/04 Javascript
Python复制目录结构脚本代码分享
2015/03/06 Python
快速解决jupyter notebook启动需要密码的问题
2020/04/21 Python
opencv 查找连通区域 最大面积实例
2020/06/04 Python
全方位了解CSS3的Regions扩展
2015/08/07 HTML / CSS
可能这些是你想要的H5软键盘兼容方案(小结)
2019/04/23 HTML / CSS
小学生检讨书大全
2014/02/06 职场文书
护理助产毕业生的求职信
2014/03/02 职场文书
高中教师考核方案
2014/05/18 职场文书
抄袭同学作业检讨书1000字
2014/11/20 职场文书
2015年个人实习工作总结
2014/12/12 职场文书
党校毕业个人总结
2015/02/28 职场文书
硕士毕业答辩开场白
2015/05/27 职场文书
2019年个人工作总结范文
2019/03/25 职场文书
图解上海144收音机
2021/04/22 无线电
css3 文字断裂效果
2022/04/22 HTML / CSS
MySql数据库触发器使用教程
2022/06/01 MySQL