smtp发送电子邮件以及为什么一个附件可以有两个Content-Type?

时间:2017-04-26 06:21:55

标签: python smtp content-type mime

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

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==--

1 个答案:

答案 0 :(得分:1)

如果查看documentation for the MIMEApplication class,您应该在构造函数中传递mime类型,而不是将其作为单独的标题添加。

Dict = {id:i for i, id in enumerate(IDs)}