Python 发email 带附件

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

[Python]代码    

#encoding=utf-8

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
    

标签: isp 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:objective-c 从网络下载图片,保存,并用 UIImageView 从保存中显示

下一篇:iPhone发送短信实现