如何阻止 GMail 将电子邮件中的嵌入图像显示为附件?

发布于 2024-09-29 06:40:13 字数 1106 浏览 2 评论 0原文

我正在发送带有嵌入图像(作为附件)的 HTML 电子邮件,并且图像按预期在 GMail 中显示。但是,它们也会显示为电子邮件下的附件。有谁知道如何避免这种情况,即我只希望它们出现在电子邮件中,而不是作为附件列出。我使用了“内容处置:内联”。我正在使用 Spring 和 JavaMail。

这是我的邮件最终的样子。任何人都可以看到出了什么问题吗?

Delivered-To: ...
...
Subject: ...
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_0_1248835444.1288246311187"

------=_Part_0_1248835444.1288246311187
Content-Type: multipart/related; 
    boundary="----=_Part_1_592250078.1288246311197"

------=_Part_1_592250078.1288246311197
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

<html>
<body>
<p><img src="cid:a0"/></p>
</body>
</html>
------=_Part_1_592250078.1288246311197
Content-Type: image/x-png
Content-Transfer-Encoding: base64
Content-Disposition: inline
Content-ID: <a0>

iVBORw0KGgoAAAANSUhEUgAAAPAAAABQCAYAAAAnSfh8AAAACXBIWXMAAC4jAAAuIwF4pT92AAAA
...
Qcz8hzyUxqGHjkNbsY4Df5iBg6OIwafQHBxFjP8PAIwl43uhncLdAAAAAElFTkSuQmCC
------=_Part_1_592250078.1288246311197--

------=_Part_0_1248835444.1288246311187--

I am sending HTML emails with embedded images (as attachments) and the images display as expected in GMail. However they also show up as attachments under the email. Does anyone know how to avoid this i.e. I want them in the email only and not listed as attachments. I have used "Content-Disposition: inline". I am using Spring and JavaMail.

Here is what my mails end up like. Can anyone see whats wrong?

Delivered-To: ...
...
Subject: ...
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_0_1248835444.1288246311187"

------=_Part_0_1248835444.1288246311187
Content-Type: multipart/related; 
    boundary="----=_Part_1_592250078.1288246311197"

------=_Part_1_592250078.1288246311197
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

<html>
<body>
<p><img src="cid:a0"/></p>
</body>
</html>
------=_Part_1_592250078.1288246311197
Content-Type: image/x-png
Content-Transfer-Encoding: base64
Content-Disposition: inline
Content-ID: <a0>

iVBORw0KGgoAAAANSUhEUgAAAPAAAABQCAYAAAAnSfh8AAAACXBIWXMAAC4jAAAuIwF4pT92AAAA
...
Qcz8hzyUxqGHjkNbsY4Df5iBg6OIwafQHBxFjP8PAIwl43uhncLdAAAAAElFTkSuQmCC
------=_Part_1_592250078.1288246311197--

------=_Part_0_1248835444.1288246311187--

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无名指的心愿 2024-10-06 06:40:13

可以按照以下步骤从 GMail 中获取这样的邮件正文:

  1. 在 Gmail 实验室中启用“Kent T 插入图像”
  2. 编写邮件并将图像嵌入到邮件中
  3. 将邮件发送给自己
  4. 打开邮件。在右上角,按带有向下箭头的图片(位于“回复”附近),然后按“显示原始内容”。您将在那里找到此类邮件的示例。

我这样做并得到以下结果,其中图像未列为附件。也许这个邮件正文可以帮助您使用 Spring 和 JavaMail 创建正确的邮件:

MIME-Version: 1.0
...
Subject: ...
From: ...
To: ...
Content-Type: multipart/related; boundary=000e0cd62fb69a9c280493a7a1c0

--000e0cd62fb69a9c280493a7a1c0
Content-Type: multipart/alternative; boundary=000e0cd62fb69a9c250493a7a1bf

--000e0cd62fb69a9c250493a7a1bf
Content-Type: text/plain; charset=ISO-8859-1

[image: abc.png]

--000e0cd62fb69a9c250493a7a1bf
Content-Type: text/html; charset=ISO-8859-1

<img src="cid:ii_12bf191c5eab934e" alt="abc.png" title="abc.png"><br>

--000e0cd62fb69a9c250493a7a1bf--
--000e0cd62fb69a9c280493a7a1c0
Content-Type: image/png; name="abc.png"
Content-Transfer-Encoding: base64
Content-ID: <ii_12bf191c5eab934e>
X-Attachment-Id: ii_12bf191c5eab934e

iVBORw0KGgoAAAANSUhEUgAAAJ4AAADLCAIAAAAQpL1oAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA
...

It is possible to get such a mail body out of GMail by following these steps:

  1. enable in Gmail Labs "Inserting images by Kent T"
  2. write an mail and embed an image into the mail
  3. send the mail to yourself
  4. open the mail. On the upper right corner, press the picture with the arrow down (right near Reply) and press "Show original". There you will find an example of such a mail.

I did this and got following result, where the image is not listed as attachement. Maybe this mail body helps you to create a proper mail with Spring and JavaMail:

MIME-Version: 1.0
...
Subject: ...
From: ...
To: ...
Content-Type: multipart/related; boundary=000e0cd62fb69a9c280493a7a1c0

--000e0cd62fb69a9c280493a7a1c0
Content-Type: multipart/alternative; boundary=000e0cd62fb69a9c250493a7a1bf

--000e0cd62fb69a9c250493a7a1bf
Content-Type: text/plain; charset=ISO-8859-1

[image: abc.png]

--000e0cd62fb69a9c250493a7a1bf
Content-Type: text/html; charset=ISO-8859-1

<img src="cid:ii_12bf191c5eab934e" alt="abc.png" title="abc.png"><br>

--000e0cd62fb69a9c250493a7a1bf--
--000e0cd62fb69a9c280493a7a1c0
Content-Type: image/png; name="abc.png"
Content-Transfer-Encoding: base64
Content-ID: <ii_12bf191c5eab934e>
X-Attachment-Id: ii_12bf191c5eab934e

iVBORw0KGgoAAAANSUhEUgAAAJ4AAADLCAIAAAAQpL1oAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA
...
攒一口袋星星 2024-10-06 06:40:13

使用 Spring 你可以这样做:

MimeMessagePreparator message = mimeMessage -> {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(new InternetAddress(emailSender));
        mimeMessageHelper.setTo("[email protected]");
        mimeMessageHelper.setSubject("A subject");
        String html = ... // contains a tag <img src='cid:logoIcon'>
        mimeMessageHelper.setText(html, true);

        mimeMessageHelper.addInline("logoIcon", new ClassPathResource("/data/images/logo.png"));
    };
mailSender.send(message);

Using Spring you can do that like this:

MimeMessagePreparator message = mimeMessage -> {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(new InternetAddress(emailSender));
        mimeMessageHelper.setTo("[email protected]");
        mimeMessageHelper.setSubject("A subject");
        String html = ... // contains a tag <img src='cid:logoIcon'>
        mimeMessageHelper.setText(html, true);

        mimeMessageHelper.addInline("logoIcon", new ClassPathResource("/data/images/logo.png"));
    };
mailSender.send(message);

月下凄凉 2024-10-06 06:40:13

对于 Gmail,请注意内容 ID 只包含字母数字字符!

我的 CID 中有空格,只有 GMAIL 无法正确显示。我删除了空格,一切正常。

** 我强烈建议使用像 md5 这样的哈希函数来命名 cid **

For Gmail, Be careful to have a content-id with only alphanumeric characters !

I had spaces in my CID and only GMAIL was not displaying correctly. I removed spaces and everything went right.

** I strongly suggest to use a hash function like md5 to name the cid **

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文