Python 邮件操作全攻略
1. 发送带附件的邮件
在 Python 中发送带附件的邮件,需要创建 MimeMultipart() 类的对象实例。若有多个附件,可以依次构建。以下是一个示例,向邮件中添加两个文本文件:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
message = MIMEMultipart()
message['From'] = Header("Sender", 'utf-8')
message['To'] = Header("Receiver", 'utf-8')
message['Subject'] = Header('Python SMTP', 'utf-8')
message.attach(MIMEText('Python SMTP', 'plain', 'utf-8'))
file1 = MIMEText(open('file1.txt', 'rb').read(), 'base64', 'utf-8')
file1["Content-Type"] = 'application/octet-stream'
file1["Content-Disposition"] = 'attachment; filename="file1.txt"'
message.attach(file1)
file2 = MIMEText(open('file2.txt', 'rb').read(), 'base6
超级会员免费看
订阅专栏 解锁全文
105

被折叠的 条评论
为什么被折叠?



