python 发送带附件的 邮件

from email.MIMETextimportMIMETextfrom email.MIMEMultipartimportMIMEMultipartimport smtplib

mail_host ='smtp.126.com'
mail_user ='xx@126.com'
mail_pwd ='xx'
mail_to ='xxzhao@gmail.com'

msg =MIMEMultipart()

att =MIMEText(open('d:\\a.txt','rb').read(),'base64','gb2312')
att["Content-Type"]='application/octet-stream'
att["Content-Disposition"]='attachment;filename="hello.txt"'
msg.attach(att)

message ='content part'
body =MIMEText(message)
msg.attach(body)
msg['To']= mail_to
msg['from']= mail_user
msg['subject']='this is a python test mail'try:
    s = smtplib.SMTP()
    s.connect(mail_host)
    s.login(mail_user,mail_pwd)

    s.sendmail(mail_user,mail_to,msg.as_string())
    s.close()print'success'exceptException,e:print e

 

posted on 2014-01-24 16:53  strikebone  阅读(149)  评论(0编辑  收藏  举报

导航