如何利用python发送邮件
作者:莫使娇躯空对月 时间:2022-11-09 09:34:31
一、zmial发送邮件
zmial是第三方库,需进行安装
pip install zmail
完成后,来给发一封邮件
subject:标题
content_text:内容
import zmail
server = zmail.server('发件人邮箱地址','授权码')
server.send_mail('收件人邮箱地址',{'subject':'Hello!','content_text':'By zmail.'})
二、smtplib发送邮件
import smtplib
from email.mime.text import MIMEText
#--------发件相关参数--------
smtpserver="smtp.qq.com" #连接服务器
port = 465 #端口
sender = "741841851@qq.com"#账号
psw = "xxxxx"#密码 授权码
receiver="741841851@qq.com"#接收人
#--------编辑邮件内容--------
subject="qq邮件主题"
body= '<p>这个是发送的qq邮件</p>'
msg = MIMEText(body,'html','utf-8')
msg['from']=sender
msg['to']='741841851@qq.com'
msg['subject']=subject
#-----------test_email-------
smtp = smtplib.SMTP_SSL(smtpserver,port)#连接服务器
smtp.login(sender,psw)#登录
smtp.sendmail(sender,receiver,msg.as_string())#发送邮件
smtp.quit()
三、发送带附件的邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
smtpserver='smtp.qq.com'
port =465
sender='741841851@qq.com'
psw = 'xxxx'
recevier = "741841851@qq.com"
filenamepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),'ceshi.html')
with open(filenamepath,'rb') as f:
mail_body=f.read().decode('utf-8')
msg = MIMEMultipart()
msg['from']=sender#发件人
msg['to']=recevier#收件人
msg['subject']='这是我的主题99'#主题
# 正文
body = MIMEText(mail_body,'html','utf-8')
msg.attach(body)
#附件
att = MIMEText(mail_body,'base64','gbk')#用utf-8会出现乱码
att['Content-Type']='application/octet-stream'
att['Content-Disposition']='attachment;filename="test_report.html"'
msg.attach(att)
####发送邮件
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver)#连接服务器
smtp.login(sender,psw)#登录
except:
smtp = smtplib.SMTP_SSL(smtpserver,port)
smtp.login(sender,psw)#登录
smtp.sendmail(sender,recevier,msg.as_string())#发送邮件
smtp.quit()
来源:https://www.cnblogs.com/xiaoyujuan/p/11065590.html
标签:python,发送,邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你用scrapy框架爬取豆瓣读书Top250的书类信息
2022-11-04 12:48:23
![](https://img.aspxhome.com/file/2023/8/118568_0s.jpg)
浅谈常用Java数据库连接池(小结)
2024-01-18 06:50:25
![](https://img.aspxhome.com/file/2023/4/79934_0s.png)
mysql数据库中字符集乱码问题原因及解决
2024-01-14 19:38:10
![](https://img.aspxhome.com/file/2023/6/126256_0s.png)
MySQL切分查询用法分析
2024-01-28 01:11:35
Ubuntu下Python+Flask分分钟搭建自己的服务器教程
2023-11-12 04:37:43
![](https://img.aspxhome.com/file/2023/2/63182_0s.jpg)
python中numpy 常用操作总结
2021-01-10 05:01:57
python项目打包成exe和安装包的方法步骤
2023-06-29 08:37:21
![](https://img.aspxhome.com/file/2023/5/112185_0s.jpg)
Python库安装速度过慢解决方案
2023-09-14 14:57:30
![](https://img.aspxhome.com/file/2023/0/125190_0s.jpg)
mysql累积聚合原理与用法实例分析
2024-01-25 14:23:43
![](https://img.aspxhome.com/file/2023/4/111394_0s.png)
用sysbench来测试MySQL的性能的教程
2024-01-16 10:56:52
jsp输出九九乘法表的简单实例
2023-07-12 01:26:31
![](https://img.aspxhome.com/file/2023/0/91750_0s.jpg)
Python系统公网私网流量监控实现流程
2022-05-16 07:55:39
![](https://img.aspxhome.com/file/2023/4/120884_0s.jpg)
windows下mysql 8.0.13 解压版安装图文教程
2024-01-17 04:03:56
![](https://img.aspxhome.com/file/2023/4/107434_0s.png)
SQL学习笔记一SQL基础知识
2011-09-30 11:24:33
Python读取配置文件的实战操作
2021-08-12 19:48:09
Python使用scipy模块实现一维卷积运算示例
2022-10-18 07:15:19
ant design vue 图片预览组件自定义样式
2023-03-14 13:11:13
![](https://img.aspxhome.com/file/2023/9/123759_0s.png)
Oracle入侵常用操作命令整理
2009-03-04 11:11:00
python实现库存商品管理系统
2023-06-01 06:37:29
![](https://img.aspxhome.com/file/2023/7/80597_0s.jpg)
解决SQLServer最大流水号的两个好方法
2009-01-13 14:15:00