Python基于smtplib模块发送邮件代码实例
作者:--TINGXIN-- 时间:2022-09-18 11:07:49
smtplib模块负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。
email模块负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。
email模块下有mime包,mime英文全称为“Multipurpose Internet Mail Extensions”,即多用途互联网邮件扩展,是目前互联网电子邮件普遍遵循的邮件技术规范。
该mime包下常用的有三个模块:text,image,multpart。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
#邮件服务器信息
smtp_server = "smtp.qq.com"
port = 465 # For starttls
sender_email = "12345689@qq.com"
password="" #get password from mailsetting
#发送邮件信息,可以发送给多个收件人
receivers=["12345689@163.com","12345689@qq.com"]
subject="This is import Python SMTP 邮件(文件传输) 多媒体测试"
# message = MIMEText(text, "plain", "utf-8") #文本邮件
message = MIMEMultipart()
message["Subject"] = Header(subject, "utf-8")
message["from"] = sender_email
message["to"] = ",".join(receivers)
# 邮件正文内容
text="""
Dear Sir:
how are you ? \n
for detail information pls refer to attach1。\n
The files you need are as followed.\n
If you have any concern pls let me known.\n
enjoy your weekend.\n
BEST REGARDS \n
"""
# message.attach(MIMEText('for detail information pls refer to attach1。\n The files you need are as followed. \n If you have any concern pls let me known. \n enjoy your weekend', 'plain', 'utf-8')
message.attach(MIMEText(text,'plain','utf-8'))
# 构造附件1
attach_file1='IMG1965.JPG'
attach1 = MIMEText(open(attach_file1, 'rb').read(), 'base64', 'utf-8')
attach1["Content-Type"] = 'application/octet-stream'
attach1["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file1)
message.attach(attach1)
# 构造附件2
attach_file2='YLJ.jpg'
attach2 = MIMEText(open(attach_file2, 'rb').read(), 'base64', 'utf-8')
attach2["Content-Type"] = 'application/octet-stream'
attach2["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file2)
message.attach(attach2)
# Try to log in to server and send email
# server = smtplib.SMTP_SSL(smtp_server,port)
server = smtplib.SMTP_SSL(smtp_server,port)
try:
server.login(sender_email, password)
server.sendmail(sender_email,receivers,message.as_string())
print("邮件发送成功!!!")
print("Mail with {0} & {1} has been send to {2} successfully.".format(attach_file1,attach_file2,receivers))
except Exception as e:
# Print any error messages to stdout
print("Error: 无法发送邮件")
print(e)
finally:
server.quit()
结果
邮件发送成功!!!
Mail with IMG1965.JPG & IMG1963.jpg has been send to ['12345689@163.com', '12345689@qq.com'] successfully.
来源:https://www.cnblogs.com/tingxin/p/12961901.html
标签:Python,smtplib,模块,邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python基于BeautifulSoup爬取京东商品信息
2021-03-15 21:52:53
![](https://img.aspxhome.com/file/2023/6/72256_0s.jpg)
ie7空格的间距要比ie6/firefox/opera的都要大
2008-05-24 16:54:00
详解Python的多任务进程
2021-08-20 19:12:48
Python动态可视化模块Pynimate初体验
2021-03-22 16:35:09
![](https://img.aspxhome.com/file/2023/5/71425_0s.gif)
利用ASP+JMAIL进行邮件群发的新思路
2008-03-20 13:30:00
python中尾递归用法实例详解
2023-10-09 06:46:15
Python yield 小结和实例
2023-07-21 15:37:39
关于Python如何安装requests库
2021-10-18 04:12:38
![](https://img.aspxhome.com/file/2023/9/72269_0s.png)
Python二叉搜索树与双向链表转换算法示例
2023-07-17 22:49:42
教你快速掌握更改表中列顺序的好方法
2008-11-27 17:14:00
如何把图片也存到数据库中去?
2009-11-06 13:56:00
Python地图四色原理的遗传算法着色实现
2022-10-07 20:57:45
![](https://img.aspxhome.com/file/2023/5/75955_0s.jpg)
对Python的交互模式和直接运行.py文件的区别详解
2023-12-18 00:03:55
Linux 发邮件磁盘空间监控(python)
2022-03-15 09:17:24
Python实现合并多张图片成视频的示例详解
2021-07-17 06:18:14
![](https://img.aspxhome.com/file/2023/1/67851_0s.jpg)
HTML5 WebSockets基础使用教程
2010-09-21 12:48:00
![](https://img.aspxhome.com/file/UploadPic/20109/21/01-97s.jpg)
网马解密大讲堂——网马解密中级篇(Freshow工具使用方法)
2009-09-16 15:09:00
![](https://img.aspxhome.com/file/UploadPic/20099/16/freshow-13s.jpg)
Python包argparse模块常用方法
2023-04-03 13:30:58
![](https://img.aspxhome.com/file/2023/1/95331_0s.png)
一篇文章搞懂Python反斜杠的相关问题
2021-11-26 17:45:19
![](https://img.aspxhome.com/file/2023/2/97662_0s.png)
pytorch之torchvision.transforms图像变换实例
2021-05-19 05:44:05