If I attach an image to an email, how can I place it in the HTML content? I tried just using the filename as the image source but that doesn't seem to work.
7 Answers
Be more specific on how you build the HTML mail message.
The result will be a multipart-MIME message with a text/html part (if you really do it right with an alternate part of type text/plain) and several images, which are then referenced from within the HTML.
See RFC 1813 and RFC 2378 for more information about content-id in mixed MIME and related data (referred by CID in the HTML source).

- 59,176
- 9
- 122
- 152
-
1It is built as a multipart MIME message with html and image parts. If I'm reading these RFCs correctly, it sounds like I just need to put a content id on the image attachments and then use cid:xxx as the image source. – user83358 May 29 '09 at 20:26
-
This is correct, if everything else is already in place in the MIME message, embedding them is easy. – Lucero Jun 02 '09 at 11:28
-
This is a pretty poor, incomplete aswwer. @deem's answer below actually answers the question; the two RFCs mentioned don't talk about HTML or URI schemes at all. – Morgan Harris Feb 11 '19 at 04:41
The image attachment section needs Content-ID
--T4nu9J8b
Content-Type: image/png
Content-ID: <idname>
Content-Transfer-Encoding: base64
Content-Disposition: attachment ;filename="testimage.png"
iVBORw0KGgoAAAANS...
--T4nu9J8b--
Note: Content-ID name should be put in an angular bracket, as given
Embed it into the tag using same Content-ID (without the angular bracket)
<img alt="Embedded Image" src="cid:idname"/>
This should allow the attached image to be displayed into the HTML!

- 718
- 2
- 9
- 25
Option 01:
- attach the file "mySignaturePictue.jpg" as attachment to the mail
- reference this attachment from the body (insert it) whith code like:
<img src="cid:mySignaturePicture.png">
Option 02:
- convert your image to a base64 string: http://www.motobit.com/util/base64-decoder-encoder.asp
- insert it to the mailtext/html-body with code like
<img alt="My Image" src="data:image/jpeg;base64,AWWhcalkjsd/beginning/RXhp/of+/long/base64cod/ZgAATU0/+BlaBlubbZ5u8/61a+Xand/much/more..." />
- Downside: this is blocked by most clients and will increase the mail size, see: https://www.campaignmonitor.com/blog/email-marketing/2013/02/embedded-images-in-html-email/ and https://www.paperstreet.com/blog/email-marketing-embeded-images-cid-what-a-mess/

- 2,366
- 1
- 24
- 27
The answer to your question is in the spring docs here.
mailSender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setFrom("me@mail.com");
message.setTo("you@mail.com");
message.setSubject("my subject");
message.setText("my text <img src='cid:myLogo'>", true);
message.addInline("myLogo", new ClassPathResource("img/mylogo.gif"));
}
});
The body of the message is one of the parts of the multipart message (note the second parameter set to true on the constructor of MimeMessageHelper that sets the message to a multipart message).
The line message.addInline("myLogo"...
adds the image as another part of the multipart message.
You can use .setText
to set the body (HTML content) of the email message.
You can refer to other parts of the multipart email (your image) by using the tag cid. Note how the img src attribute is src='cid:myLogo'
. cid is the content id of the image, sent as one of the parts of the multipart message.

- 111
- 3
-
-
1It looks like `message.addInline` needs to go after `message.setText` or the file won't be attached to the email – Tk421 Jun 03 '19 at 08:30
I'm doing it in this way
_mime = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("C:\\bgBoletin.jpg");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<bgBoletin>");
_mime.addBodyPart(messageBodyPart);
And in HTM file whe reference de Content-ID in this way background: url(cid:bgBoletin).

- 17,397
- 7
- 55
- 80

- 211
- 4
- 10
If this has to do with sending emails from a website you are developing, just keep the images on your server and then link to them using the <img src="url"/>
.

- 37,429
- 10
- 86
- 110
-
15...most modern mail applications do't show those images for security reasons unless explicitly confirmed by the user. Embedding (small) images is much better in order to make sure that the message will show up as intended (or, this will be more likely). – Lucero May 28 '09 at 20:22
-
1@Lucero actively circumventing application behavior is taboo, in my opinion. The user has chosen to use a given application __because__ of its features, and may in fact be offended that someone has gone out of their way to bypass this functionality. Most modern mail apps also give you the option of always viewing images from certain senders (or even domains), letting the user decide if they want to see your images in the future. – Nathan Cox Mar 14 '12 at 15:23
-
2@NathanCox, it's not at all about bypassing the functionality. E-mail clients don't download images for protecting the users from being exposed (when the web address is personalized, the sender could find out when the mail was read and also that the e-mail address is still valid and in use). Not displaying the linked images is a security measure and has nothing to do with the users preference of seeing images (that is a separate setting in some e-mail clients). – Lucero Mar 14 '12 at 17:01
-
@Lucero I guess I've never used a client that had the two options separated. My experience has always been that if you want to disable images from downloading (e.g., if you're on a pay-for-bandwidth connection such as an air card or mobile phone) the option ties directly in with the security feature. – Nathan Cox Mar 14 '12 at 17:07
-
1@NathanCox, on pay-for-bandwidth connections you usually control whether attachments shall be downloaded. The emdedded images are MIME attachments (without filename but with an ID instead) so that e-mail clients should download the text only as well when your settings says not to download attachments. – Lucero Mar 14 '12 at 21:36