Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("your@email.com"));
Transport.send(m)

Let me know if that works for you! ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...