java 邮件 嵌入图片,使用JavaMail在电子邮件中嵌入图像

本文档解决了使用JavaMail发送包含内联图片的电子邮件时遇到的问题。通过创建MimeMultipart内容,设置正确的Content-ID和Content-Disposition,以及在HTML正文中引用Content-ID来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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?

解决方案

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 ?

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

+ ""

+ "

"

+ "

Here is my image:

"

+ " %5C%22cid:%22"

+ " "

+ ""

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

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! ;)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值