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. mysql - AttributeError: ’module’ object has no attribute ’MatchType’2. javascript - JS設置Video視頻對象的currentTime時出現了問題,IE,Edge,火狐,都可以設置,反而chrom卻...3. javascript - 圖片能在網站顯示,但控制臺仍舊報錯403 (Forbidden)4. MySQL客戶端吃掉了SQL注解?5. 網頁爬蟲 - python爬蟲翻頁問題,請問各位大神我這段代碼怎樣翻頁,還有價格要登陸后才能看到,應該怎么解決6. 數據庫 - MySQL 單表500W+數據,查詢超時,如何優化呢?7. objective-c - iOS怎么實現像QQ或者微信的實時推送8. php自學從哪里開始?9. 求大神幫我看看是哪里寫錯了 感謝細心解答10. phpstady在win10上運行
