简体   繁体   中英

How to stop embedded images in email being displayed as attachments by GMail?

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

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

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("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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM