登录 注册

Python发email 带附件

收藏
[Python] 标签: 2012-08-31 20:43
原始代码 全屏查看 0评 / 0藏 / 6347阅  跳至 / 35行
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import 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'
except Exception,e:
    print e

最新评论

  · · · · · ·  (共0条)

目前还没有评论

登录后您才可以发表评论。 马上登录 立即注册
cottage
2012-02-29加入
一段精湛的代码,天涯何处寻知音!
Back to Top