python自动发送带附件和多位收件人的邮件(163邮箱,亲测可用)

1.新建py脚本,sendmail.py 

import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 创建一个带附件的邮件实例
message=MIMEMultipart()
# 邮件的其他属性
receiver = ["[email protected]","[email protected]"]
message['From'] = '<[email protected]>'
message['Subject'] = Header(u'邮件标题发送成功', 'utf8').encode()
message['To'] = ','.join(receiver)
# 邮件正文内容
attr2 = MIMEText('恭喜您查收到该邮件', 'plain', 'utf-8')
message.attach(attr2)
#构造附件txt附件1
attr1=MIMEText(open(r'D:\j\run1.txt','rb').read(),'base64','utf-8')
attr1["content_Type"]='application/octet-stream'
attr1["Content-Disposition"] = 'attachment; filename="run1.txt"' 
message.attach(attr1)
server = smtplib.SMTP('smtp.163.com', 25)
server.login('[email protected]', '独立密码')
server.sendmail('[email protected]', receiver,message.as_string())
print("邮件发送成功")

2.运行python

打开cmd窗口,进入py文件保存路径下,输入文件名,点击回车

你可能感兴趣的:(python)