2012-10-30 84 views
0

我正在學習javamail API。要發送包含圖片的電子郵件,我使用imgPart.setHeader(「Content-ID」,「img」)。收件人將在上面看到帶有圖像和圖像的郵件內容。但是當我編寫郵件包含使用Outlook的圖像時,收件人將看不到任何附加圖像,只能看郵件文本和嵌入圖像。 Outlook如何做到這一點?發送電子郵件包含像Outlook這樣的圖像嗎?

謝謝。

回答

1

除了純文本之外,Outlook和其他類似客戶端嵌入了HTML版本的文本。在html中,他們使用img標記(當您嵌入圖像本身時設置)引用帶有ID的圖像。圖像已經分開嵌入,就像你已經做的那樣。

但我建議你使用比'img'(不要與上面提到的標籤混淆)part-ID(f.ex. hash原始文件名)更獨特的ID。

更新:

要更清楚地顯示這裏是一個例子。可以說,我發這封郵件:

Snapshot of email sent

這會給這個源發送的SMTP - 在這裏你看到的第一部分是純文本格式:

...other header parts snipped 
Subject: Demo attachments 
Content-Type: multipart/alternative; 
boundary="------------080308060008080306040307" 

This is a multi-part message in MIME format. 
--------------080308060008080306040307 
Content-Type: text/plain; charset=ISO-8859-1; format=flowed 
Content-Transfer-Encoding: 7bit 

This text is attached as HTML. As we reference an image this will be 
displayed here as well as being an attachment as usual. 



End of message 


--------------080308060008080306040307 

是雷鳥之後(在此與Outlook相同)插入相同文本的HTML版本,其編輯位於:

Content-Type: multipart/related; 
boundary="------------080209080402080405070800" 


--------------080209080402080405070800 
Content-Type: text/html; charset=ISO-8859-1 
Content-Transfer-Encoding: 7bit 

<html> 
    <head> 

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> 
    </head> 
    <body text="#000000" bgcolor="#FFFFFF"> 
    This text is attached as HTML. As we reference an image this will be 
    displayed here as well as being an attachment as usual.<br> 
    <br> 
    <img src="cid:[email protected]" alt=""><br> 
    <br> 
    End of message<br> 
    <br> 
    </body> 
</html> 

--------------080209080402080405070800 

請注意以上圖像的鏈接(I scra mbled與X的域名):

<img src="cid:[email protected]" alt=""> 

如果你現在看在郵件正文中的附件的其餘部分,你會看到Content-ID具有相同的ID在HTML中引用 - 本身附於圖像通常以Base64格式。參考鏈接始終以cid:<attachement ID>

ID可以是任何不會與身體其他部分發生碰撞的東西。

另請注意,Content-Disposition行指示Thunderbird以內聯方式顯示它,並非嚴格必要,因爲它在HTML中被引用,但可幫助客戶知道該圖像的意圖是將其顯示爲內聯還是使其可用下載一個 「純粹」 的附件:

Content-Type: image/png; 
name="ddebjigc.png" 
Content-Transfer-Encoding: base64 
Content-ID: <[email protected]> 
Content-Disposition: inline; 
filename="ddebjigc.png" 

iVBORw0KGgoAAAANSUhEUgAAANsAAAAcCAIAAABnDF0bAAASPElEQVR4nO2b+VMTWbvHc/+I 
+TNu6SiyyL4lyL4qm4gIoigiO4ii4OCGsoR9QAQZkRAC4uj4jigZRRyZV3iRfQ+BrJ3uQMjG 
Ork/dLrTaxJmbt2aqkvXt6ic06efX/jUs5zzHEZ6WuKBDvTPEePx49YDHeifIwYIQQRBSghS 

...剪斷縮短在這個例子中的文字...

sFjyaDrJSZ6SMnZjnCKphQLLIrYhUgGSPSj6IbHoJl2HxYVvEojkezZ0oo3jlD4ScwvW2EhB 
c/8Vv4zUZE66LIuGXdOdV1x0xl/H+Zs+EjNEnCKRMPy8pfs3GKdIaKPEOUXSegUikGFNJ/rB 
c/D8nz3/A+4rfUt5pwb4AAAAAElFTkSuQmCC 
--------------080209080402080405070800-- 

--------------080308060008080306040307-- 

希望這使得它更清晰。

+0

感謝,但爲什麼Outlook發送圖像而沒有任何附件?你能更清楚地告訴我嗎? – user1780606

+0

我更新了答案,使其更加清晰。 – K3N

+0

謝謝,我明白你的意思。 – user1780606

0
   File template = new File ("template.htm"); 
     FileUtils readContent = new FileUtils(); 
     String content = readContent.readFileToString (template); 

     MimeMultipart multiPart = new MimeMultipart ("related"); 
     BodyPart bodyPart = new MimeBodyPart(); 
     bodyPart.setContent (content, "text/html"); 
     multiPart.addBodyPart (bodyPart); 
     BodyPart imgPart = new MimeBodyPart(); 
     DataSource img_data = new FileDataSource ("template\\image001.jpg"); 
     imgPart.setDataHandler (new DataHandler (img_data)); 
     imgPart.setDisposition (MimeBodyPart.INLINE); 
     imgPart.setHeader ("Content-ID", "img_1"); 
     multiPart.addBodyPart (imgPart); 
相關問題