smtp发送电子邮件,为什么一个附件可以有两个Content-Type? [英] smtp send email and why one attachment can have two Content-Type?

查看:1012
本文介绍了smtp发送电子邮件,为什么一个附件可以有两个Content-Type?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用smtp发送带有附件的电子邮件.当我收到原始电子邮件时,一个附件有两种内容类型. 我怎样才能获得一种内容类型,而这两种类型会互相影响呢? 感谢您的帮助!

I'm trying use smtp to send email with attachment.And when I get raw email,there are two content-type for one attachment. How can I just get one content-type?And the two type impact each other? Thanks for any help!

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

server = smtplib.SMTP()
server.connect("smtp.XX.com")

server.login("","")

msg = MIMEMultipart("")
msg['From'] = ""
msg['Subject'] = "titlesub"
part = MIMEApplication(open("D:\data.txt", 'rb').read())
filename="data.txt"
#part['Content-Type']="application/pdf"
part.add_header('Content-Type','application/pdf')
part.add_header('Content-Disposition', 'attachment', filename=filename)

msg.attach(part)
msg['To'] = ""
server.send_message(msg)
server.quit()

原始电子邮件:

Received: from [127.0.0.1] (unknown [101.81.225.242])
by smtp8 (Coremail) with SMTP id DMCowABH3zUeOgBZsU+uAg--.2242S2;
Wed, 26 Apr 2017 14:11:42 +0800 (CST)
Content-Type: multipart/; boundary="===============4516509904929376112=="
MIME-Version: 1.0
From: 
Subject: titlesub
To: 
X-CM-TRANSID:DMCowABH3zUeOgBZsU+uAg--.2242S2
Message-Id:<59003A1E.C4DB82.14752@m12-12.163.com>
X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73
VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUkebkUUUUU
X-Originating-IP: [101.81.225.242]
Date: Wed, 26 Apr 2017 14:11:42 +0800 (CST)
X-CM-SenderInfo: pix130tbbsiiqu6rljoofrz/1tbivh7F0FZcM5OV1wAAsd

--===============4516509904929376112==
Content-Type: application/octet-stream
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Type: application/pdf
Content-Disposition: attachment; filename="data.txt"

77u/

--===============4516509904929376112==--

推荐答案

如果您查看

If you look at the documentation for the MIMEApplication class, you should be passing the mime type in the constructor, not adding it as a separate header.

part = MIMEApplication(open("file.pdf", 'rb').read(), 'pdf')
filename="file.pdf"
part.add_header('Content-Disposition', 'attachment', filename=filename)

这篇关于smtp发送电子邮件,为什么一个附件可以有两个Content-Type?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆