python 发邮件
问题描述
用python 发送带zip格式的邮件,邮件发送成功了,但是附件打不开是,代码如下
import smtplibfrom email import encodersfrom email.mime.base import MIMEBasefrom email.mime.multipart import MIMEMultipart
def send_file_zipped(the_file):
themsg = MIMEMultipart()themsg[’Subject’] = the_filethemsg[’to’] = ’xxx’themsg[’from’] = ’xxx’themsg.preamble = the_filemsg = MIMEBase(’application’, ’zip’)zf = open(the_file + ’.zip’, ’rb’)msg.set_payload(zf.read())encoders.encode_base64(msg)msg.add_header(’Content-Disposition’, ’attachment’, filename=the_file + ’.zip’)themsg.attach(msg)themsg = themsg.as_string()try: server = smtplib.SMTP() server.timeout = 30 server.connect(’smtp.exmail.qq.com’) server.login(’xxx’, ’xxx’) server.sendmail(’xxx’, ’xxx’, themsg) server.quit() print ’发送成功’except Exception, e: print str(e)
if name == '__main__':
file = ’20170305’send_file_zipped(file)
找了好多方法,都是这个结果,请教各位是哪里出了问题,邮件附件显示如下:
问题解答
回答1:你可以试一下我写的这个,用的是新浪邮箱发的,在我这儿是无论什么附件格式都可以发
相关文章:
1. java - mybatis怎么实现在数据库中有就修改,没有就添加2. MYSQL新建用户设置可以远程访问的问题3. $fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题4. python - 求一个在def中可以实现调用本def满足特定条件continue效果的方法(标题说不太清楚,请见题内描述)5. angular.js - angularjs的自定义过滤器如何给文字加颜色?6. javascript - 用表单提交两个时间段请求后台返回对应数据时出现的一些问题!7. node.js - nodejs和前端JavaScript 字符串处理结果不一样是什么原因?8. mysql - 我的myeclipse一直连显示数据库连接失败,不知道为什么9. mysql - SQL操作时间的函数?10. mysql 为何insert的时候会有lock wait timeout 异常
![$fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题](http://www.haobala.com/attached/image/news/202205/093622cb60.png)