import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication
USER = '1861825XXX@163.com' PASSWORD = 'XXXhaoqwe123' # 如名字所示: Multipart就是多个部分 msg = MIMEMultipart() HOST = 'smtp.163.com' msg['subject'] = 'test email from python' msg['to'] = 'XXX@126.com' msg['from'] = '1861825XXX@163.com' text = MIMEText('我是纯文本') msg.attach(text) #添加附件1 xlsxpart = MIMEApplication(open('test1.xlsx', 'rb').read()) xlsxpart.add_header('Content-Disposition', 'attachment', filename='test1.xlsx') msg.attach(xlsxpart) #添加附件2 xlsxpart2 = MIMEApplication(open('test2.xlsx', 'rb').read()) xlsxpart2.add_header('Content-Disposition', 'attachment', filename='test2.xlsx') msg.attach(xlsxpart2) #开始发送邮件 client = smtplib.SMTP() client.connect(HOST) client.login(USER, PASSWORD) client.sendmail('1861825XXXX@163.com', ['XXXX@126.com'], msg.as_string()) client.quit() print('发送成功........')
|