PYTHON email包发送邮件的用法

from email.Header import Header
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import smtplib, datetime

#创建一个带附件的实例
msg = MIMEMultipart()
#构造附件
att = MIMEText('无效', 'plain', 'gb2312')
msg.attach(att)
path = r'D:\Downloads\限量.xls'
att = MIMEApplication(open(path, 'rb').read(), 'ms-excel')
att["Content-Disposition"] = 'attachment; filename="限量.xls"'
msg.attach(att)
path = r'D:\Downloads\限量1.xls'
att = MIMEApplication(open(path, 'rb').read(), 'ms-excel')
att["Content-Disposition"] = 'attachment; filename="限量1.xls"'
msg.attach(att)

#加邮件头
msg['to'] = '1501942xxxx <[email protected]>'
msg['from'] = '张三 <[email protected]>'
msg['subject'] = Header('无效 (2011-12-20)', 'gb2312')
msg['date'] = 'Thu, 1 Dec 2011 16:43:45 +0800'
#发送邮件
server = smtplib.SMTP('mx2.mail.139.com')
#server.login('1501942xxxx','***')
#server.set_debuglevel(1)
server.sendmail('[email protected]', msg['to'], msg.as_string())
server.close()
 

猜你喜欢

转载自lht.iteye.com/blog/1336537
今日推荐