python 发送邮件,并且带附件

#!/usr/bin/python
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
msg = MIMEMultipart()
att1 = MIMEText('</pre> <h1>nnn</h1> <pre>','html','utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="month_report.xls"'
msg.attach(att1)
msg['to'] = 'xx@163.com'
msg['from'] = 'xx@163.com'
msg['subject'] = 'hello world'
try:
server = smtplib.SMTP()
server.connect('smtp.163.com')
server.login('xxx@163.com','xxxxxx')                              ##:如果采用163的邮箱,  实用的密码不是网页ssl登录的密码,是需要填写163邮箱中支持第三方smtp的密码                        
server.sendmail(msg['from'], msg['to'],msg.as_string())
server.quit()
print 'success'
except Exception, e:
print str(e)

posted @ 2018-03-09 13:50  xiao_pai_pai  阅读(147)  评论(0编辑  收藏  举报