发送电子邮件包括像Outlook这样的图像? [英] Send email includes image like Outlook does?

查看:250
本文介绍了发送电子邮件包括像Outlook这样的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习javamail API。要发送电子邮件包含图像,我使用imgPart.setHeader(Content-ID,img)。收件人将看到附有图像和图像的邮件内容。但是当我撰写电子邮件包含使用Outlook的图像时,收件人将看不到任何附加的图像,只看邮件文本和嵌入图像。 Outlook如何做?



谢谢。

解决方案

和其他类似的客户端嵌入文本的HTML版本以及纯文本。然后,在html中,然后使用 img 标签(在嵌入图像本身时设置),使用它的ID引用图像。图像是分开嵌入的。



但是我建议您使用比img更独特的ID(不要与上面提到的标签混淆) )for part-ID(f.ex. hash原始文件名)。



更新:



为了更清楚地显示这里的一个例子。假设我发送了这封电子邮件:





这将通过SMTP发送这个源 - 在这里你注意到第一部分是纯文本:

  ...其他标题部分剪切
主题:演示附件
内容类型:multipart / alternative;
boundary =------------ 080308060008080306040307

这是MIME格式的多部分消息。
-------------- 080308060008080306040307
内容类型:text / plain;字符集= ISO-8859-1; format = flowed
Content-Transfer-Encoding:7bit

此文本以HTML格式附加。当我们引用图像时,这里将显示
以及像往常一样的附件。



消息结束


-------------- 080308060008080306040307
Thunderbird之后(在这种情况下与Outlook相同)插入一个HTML版本的相同的文本,它被编辑in:

 内容类型:multipart / related; 
boundary =------------ 080209080402080405070800


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

< html>
< head>

< meta http-equiv =content-typecontent =text / html; charset = ISO-8859-1>
< / head>
< body text =#000000bgcolor =#FFFFFF>
此文本以HTML格式附加。当我们引用图像时,这里将显示
以及像往常一样的附件。< br>
< br>
< img src =cid:part1.03070608.09040802@xxxxxxxxxxxxxxxxalt =>< br>
< br>
消息结束< br>
< br>
< / body>
< / html>

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

注意上面图片的链接(我用x's加了域名):

 < img src =cid: part1.03070608.09040802@xxxxxxxxxxxxxxxxalt => 

如果您现在查看邮件正文中的其余附件,您将看到 Content-ID 具有与HTML中引用的相同的ID - 图像本身像以前一样以Base64格式附加。参考链接始终以 cid开头:< attachement ID>



ID可以是任何不会发生冲突的东西与其他部位在体内。



还注意到 Content-Disposition 行,指示Thunderbird内联显示,不是绝对必要的,因为它在HTML中引用,但是有助于客户知道这个图像的意图是将其显示为内联还是使其可以作为纯附件下载:

  Content-Type:image / png; 
name =ddebjigc.png
内容传输编码:base64
Content-ID:< part1.03070608.09040802@xxxxxxxxxxxxxxxx>
内容处理:inline;
的文件名= ddebjigc.png

iVBORw0KGgoAAAANSUhEUgAAANsAAAAcCAIAAABnDF0bAAASPElEQVR4nO2b + VMTWbvHc / + I
+ TNu6SiyyL4lyL4qm4gIoigiO4ii4OCGsoR9QAQZkRAC4uj4jigZRRyZV3iRfQ + BrJ3uQMjG
奥克/ dLrTaxJmbt2aqkvXt6ic06efX / jUs5zzHEZ6WuKBDvTPEePx49YDHeifIwYIQQRBSghS

...在这个例子中剪辑缩短文字...

  sFjyaDrJSZ6SMnZjnCKphQLLIrYhUgGSPSj6IbHoJl2HxYVvEojkezZ0oo3jlD4ScwvW2EhB 
C / 8Vv4zUZE66LIuGXdOdV1x0xl / H + ZS + EjNEnCKRMPy8pfs3GKdIaKPEOUXSegUikGFNJ / RB
C / D8nz3 / A + 4rfUt5pwb4AAAAAElFTkSuQmCC
------------- -080209080402080405070800--

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

希望这更清楚。


I'm learning about javamail API. To send an email includes image, I use imgPart.setHeader ("Content-ID", "img"). The recipient will see mail content with image and image attached above. But when I compose email includes image using Outlook, the recipient won't see any attached image, just see mail text and embedded image. How can Outlook do that ?

Thank you.

解决方案

Outlook and other similar clients embed HTML version of the text in addition to the plain text. In the html they then reference the image with it's ID using the img tag (that you set when embedding the image itself). The image is embedded separately as you already do.

But I would recommend you use a more unique ID than 'img' (not to be confused with the tag I mention above) for the part-ID (f.ex. hash the original file name).

Update:

To show more clearly here's an example. Lets say I sent this email:

This will give this source sent over the SMTP - here you notice the first part is in plain text:

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

After that Thunderbird (in this case, same with Outlook) inserts a HTML version of the same text, which it was edited in:

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:part1.03070608.09040802@xxxxxxxxxxxxxxxx" alt=""><br>
    <br>
    End of message<br>
    <br>
  </body>
</html>

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

Notice the link for the image above (I scrambled the domain with x's):

<img src="cid:part1.03070608.09040802@xxxxxxxxxxxxxxxx" alt="">

If you now look at the rest of the attachments in the message body, you will see that Content-ID has the same ID as referenced in the HTML - the image itself is attached as usual in Base64 format. The reference link always starts with cid:<attachement ID>

ID can be anything that doesn't collide with other parts in the body.

Also notice the Content-Disposition line which instruct Thunderbird to display it inline, not strictly necessary as it is referenced in the HTML, but helps the client to know if the intention with this image was to show it inline or make it available to download as a "pure" attachment:

Content-Type: image/png;
 name="ddebjigc.png"
Content-Transfer-Encoding: base64
Content-ID: <part1.03070608.09040802@xxxxxxxxxxxxxxxx>
Content-Disposition: inline;
 filename="ddebjigc.png"

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

...snipped to shorten text in this example...

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

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

Hope this made it clearer.

这篇关于发送电子邮件包括像Outlook这样的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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