python3 发送任意文件邮件的实例
作者:zhishiqu 时间:2023-12-31 15:54:42
实例如下所示:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import email.mime.multipart
import email.mime.text
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def send_email(smtpHost, sendAddr, password, recipientAddrs, subject='', content=''):
msg = email.mime.multipart.MIMEMultipart()
msg['from'] = sendAddr
msg['to'] = recipientAddrs
msg['subject'] = subject
content = content
txt = email.mime.text.MIMEText(content, 'plain', 'utf-8')
msg.attach(txt)
# 添加附件,传送D:/mydev/yasuo.rar文件
part = MIMEApplication(open('D:/mydev/6.rar','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="yasuo.rar")
msg.attach(part)
smtp = smtplib.SMTP()
smtp.connect(smtpHost, '25')
smtp.login(sendAddr, password)
smtp.sendmail(sendAddr, recipientAddrs, str(msg))
print("发送成功!")
smtp.quit()
try:
subject = 'Python 测试邮件'
content = '这是一封来自 Python 编写的测试邮件。'
send_email('smtp.163.com', '18310161797@163.com', '邮箱密码', '526189064@qq.com', subject, content)
except Exception as err:
print(err)
来源:http://blog.csdn.net/zhishiqu/article/details/79132501
标签:python3,发送,文件,邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SNS用户体验和互动性浅析
2011-01-17 17:56:00
![](https://img.aspxhome.com/file/UploadPic/20111/17/01-46s.jpg)
python3实现域名查询和whois查询功能
2023-09-19 19:35:12
Git 教程之工作流程详解
2022-03-28 13:04:45
![](https://img.aspxhome.com/file/2023/0/132310_0s.png)
Oracle11g简单安装和使用教程
2024-01-18 19:28:47
MySQL使用变量实现各种排序
2024-01-22 10:36:14
Go中函数的使用细节与注意事项详解
2023-10-15 01:02:32
![](https://img.aspxhome.com/file/2023/6/103056_0s.jpg)
js 兼容多浏览器的回车和鼠标焦点事件代码(IE6/7/8,firefox,chrome)
2024-04-23 09:24:07
python中IO流和对象序列化详解
2023-07-02 17:59:22
Go语言学习之指针的用法详解
2024-02-12 06:56:10
在Python的Django框架中实现Hacker News的一些功能
2023-11-24 19:27:46
SpringSecurity页面授权与登录验证实现(内存取值与数据库取值)
2024-01-22 20:17:54
![](https://img.aspxhome.com/file/2023/9/72289_0s.png)
如何将sql执行的错误消息记录到本地文件中实现过程
2024-01-22 06:50:37
![](https://img.aspxhome.com/file/2023/0/122400_0s.jpg)
Python实现随机生成迷宫并自动寻路
2023-11-18 11:12:41
![](https://img.aspxhome.com/file/2023/1/128181_0s.jpg)
一个用Ajax做的用户名验证程序
2007-10-21 20:40:00
如何定义记录集打开的游标类型?
2009-11-15 20:19:00
Python minidom模块用法示例【DOM写入和解析XML】
2021-04-22 23:28:19
使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)
2023-07-01 06:32:45
python matplotlib 画dataframe的时间序列图实例
2023-05-17 00:12:34
在Linux系统上部署Apache+Python+Django+MySQL环境
2024-01-14 00:51:15
![](https://img.aspxhome.com/file/2023/9/131449_0s.png)
利用Python绘制多种风玫瑰图
2023-05-05 03:41:50
![](https://img.aspxhome.com/file/2023/6/99266_0s.png)