我正在发送带有嵌入图像(作为附件)的HTML电子邮件,并且图像会在GMail中按预期显示。但是,它们也会在电子邮件下显示为附件。有谁知道如何避免这种情况,即我只想在电子邮件中使用它们而不是作为附件列出。我使用过“Content-Disposition:inline”。我正在使用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--
答案 0 :(得分:5)
可以通过以下步骤从GMail获取此类邮件正文:
我这样做并获得了以下结果,其中图片未列为附件。也许这个邮件正文可以帮助您使用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
...
答案 1 :(得分:0)
使用Spring,您可以这样做:
MimeMessagePreparator message = mimeMessage -> {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.setFrom(new InternetAddress(emailSender));
mimeMessageHelper.setTo("user@server.com");
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);
答案 2 :(得分:0)
对于 Gmail,注意内容 ID 只包含字母数字字符!
我的 CID 中有空格,只有 GMAIL 显示不正确。我删除了空格,一切顺利。
** 我强烈建议使用像 md5 这样的哈希函数来命名 cid **