python实现带附件的邮件发送基于smtp协议
#python实现发送邮件带附件 from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib #创建一个带附件的实例 msg = MIMEMultipart() #构造附件1 file1 = MIMEText(open('E:\\桌面存储位置\\计算18-9班冯磊 实验2.zip','rb').read(), 'base64', 'utf-8') file1["Content-Type"] = 'application/octet-stream' file1["Content-Disposition"] = 'attachment; filename="a.zip"' #这里的filename可以任意写,写什么名字,邮件中显示什么名字 msg.attach(file1) #加邮件头 msg['To'] = '***@qq.com' msg['From'] = '**@qq.com' msg['subject'] = 'python' #发送邮件 server = smtplib.SMTP_SSL('pop.qq.com',465) server.login('****@qq.com','*****')#XXX为用户名,XXXXX为密码 server.sendmail(msg['From'], msg['To'],msg.as_string()) server.quit()