Python发可以带附件的email操作方法分享,pythonemail,Python发可以带附件


Python发可以带附件的email操作方法分享,给需要自动发送带附件形式的朋友们一个参考。

#encoding=utf-8from email.MIMEText import MIMETextfrom email.MIMEMultipart import MIMEMultipartimport smtplibmail_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_tomsg['from'] = mail_usermsg['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#www.iplaypy.com    

Python邮件相关操作文章推荐:
把Gmail邮件转发到gtalk的Python方法
Python简易邮件查看器源码示例详解
如何用Python实现Gmail发信的方法

编橙之家文章,

评论关闭