0
我試圖使用smtp發送帶附件的電子郵件。當我收到原始電子郵件時,有一種附件有兩種內容類型。 我怎樣才能得到一種內容類型?這兩種類型相互影響? 感謝您的幫助!smtp發送電子郵件和爲什麼一個附件可以有兩個Content-Type?
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:<[email protected]>
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==--
如果我使用part ['Content-Type'] ='application/pdf',header dict中的Content-Type(application/octet-stream)不會被改變嗎? – Bella
application/octet-stream是沒有已知MIME類型時的默認值。在這種情況下,您知道您正在附加pdf,因此您應該使用pdf MIME類型。請參閱:https://tools.ietf.org/rfc/rfc3778 – gview
我嘗試它(part = MIMEApplication(open(「D:\ data.txt」,'rb')。read(),'application/pdf') )!而附件內容類型更改爲應用程序/應用程序! – Bella