Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline images in email using JavaMail

I want to send an email with an inline image using javamail.

I'm doing something like this.

MimeMultipart content = new MimeMultipart("related");  BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(message, "text/html; charset=ISO-8859-1"); content.addBodyPart(bodyPart);  bodyPart = new MimeBodyPart(); DataSource ds = new ByteArrayDataSource(image, "image/jpeg"); bodyPart.setDataHandler(new DataHandler(ds)); bodyPart.setHeader("Content-Type", "image/jpeg; name=image.jpg"); bodyPart.setHeader("Content-ID", "<image>"); bodyPart.setHeader("Content-Disposition", "inline"); content.addBodyPart(bodyPart);  msg.setContent(content); 

I've also tried

    bodyPart.setHeader("inline; filename=image.jpg"); 

and

    bodyPart.setDisposition("inline"); 

but no matter what, the image is being sent as an attachment and the Content-Dispostion is turning into "attachment".

How do I send an image inline in the email using javamail?

like image 444
akula1001 Avatar asked Jun 08 '10 10:06

akula1001


People also ask

How do I insert an image into an HTML email template?

Click on Edit HTML Version. Click in the body of the email and place your cursor where you want to image to appear. Click on the Insert an Image button in the Formatting Controls bar. - The Insert an Image button is the small picture of a tree and cloud.

Is JavaMail deprecated?

The App Engine Mail API (which also supports JavaMail) has already been deprecated. Instead, GCP recommends to use a third-party mail provider such as: SendGrid.

How do I send an email using JavaMail?

Steps to send email using JavaMail APIGet the session object that stores all the information of host like host name, username, password etc. compose the message. send the message.


1 Answers

Your problem

As far as I can see, it looks like the way you create the message and everything is mostly right! You use the right MIME types and everything.

I am not sure why you use a DataSource and DataHandler, and have a ContentID on the image, but you need to complete your question for me to be able to troubleshoot more. Especially, the following line:

bodyPart.setContent(message, "text/html; charset=ISO-8859-1"); 

What is in message? Does it contains <img src="cid:image" />?

Did you try to generate the ContentID with String cid = ContentIdGenerator.getContentId(); instead of using image


Source

This blog article taught me how to use the right message type, attach my image and refer to the attachment from the HTML body: How to Send Email with Embedded Images Using Java


Details

Message

You have to create your content using the MimeMultipart class. It is important to use the string "related" as a parameter to the constructor, to tell JavaMail that your parts are "working together".

MimeMultipart content = new MimeMultipart("related"); 

Content identifier

You need to generate a ContentID, it is a string used to identify the image you attached to your email and refer to it from the email body.

String cid = ContentIdGenerator.getContentId(); 

Note: This ContentIdGenerator class is hypothetical. You could create one or inline the creation of content IDs. In my case, I use a simple method:

import java.util.UUID;  // ...  String generateContentId(String prefix) {      return String.format("%s-%s", prefix, UUID.randomUUID()); } 

HTML body

The HTML code is one part of the MimeMultipart content. Use the MimeBodyPart class for that. Don't forget to specify the encoding and "html" when you set the text of that part!

MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setText(""   + "<html>"   + " <body>"   + "  <p>Here is my image:</p>"   + "  <img src=\"cid:" + cid + "\" />"   + " </body>"   + "</html>"    ,"US-ASCII", "html"); content.addBodyPart(htmlPart); 

Note that as a source of the image, we use cid: and the generated ContentID.

Image attachment

We can create another MimeBodyPart for the attachment of the image.

MimeBodyPart imagePart = new MimeBodyPart(); imagePart.attachFile("resources/teapot.jpg"); imagePart.setContentID("<" + cid + ">"); imagePart.setDisposition(MimeBodyPart.INLINE); content.addBodyPart(imagePart); 

Note that we use the same ContentID between < and > and set it as the image's ContentID. We also set the disposition to INLINE to signal that this image is meant to be displayed in the email, not as an attachment.

Finish message

That's it! If you create an SMTP message on the right session and use that content, your email will contain an embedded image! For instance:

SMTPMessage m = new SMTPMessage(session); m.setContent(content); m.setSubject("Mail with embedded image"); m.setRecipient(RecipientType.TO, new InternetAddress("[email protected]")); Transport.send(m) 

Let me know if that works for you! ;)

like image 177
snooze92 Avatar answered Sep 22 '22 21:09

snooze92