java 使用 Javamail 将图像直接插入 HTML 模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5315834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:35:28  来源:igfitidea点击:

Insert Image Directly into an HTML Template Using Javamail

javahtmlcssimagejavamail

提问by B.Z.B

Hey all, I've seen a lot of topics on this, but not quite what I am looking for.

大家好,我已经看到很多关于此的主题,但并不是我想要的。

Essentially when I get to sending my Javamail message I'll have my image as a byte[] object and I'll have a string that contains the html template. What I am looking to do is not store it on the server (didn't want to try to deal with the upkeep on keeping the image stored on the server, and we'll have limited space to work with). I'd like to take the byte[] object that I already have and directly store it within the html template, making sure it's in the correct image tag. Is there a way I could do this? Basically I want to stick a message.setContent("blah","image/jpg"); directly into the html template at a specific spot.

基本上,当我开始发送我的 Javamail 消息时,我将我的图像作为一个 byte[] 对象,我将有一个包含 html 模板的字符串。我想要做的不是将它存储在服务器上(不想尝试处理将图像存储在服务器上的维护工作,我们的工作空间有限)。我想获取我已经拥有的 byte[] 对象并将其直接存储在 html 模板中,确保它位于正确的图像标签中。有没有办法做到这一点?基本上我想贴一个 message.setContent("blah","image/jpg"); 直接进入特定位置的 html 模板。

Hopefully I'm making sense here...

希望我在这里说得通...

Another Idea I was thinking was add the image as an attachment and just reference the attachment when displaying the html template....if that is possible.

我在想的另一个想法是将图像添加为附件,并在显示 html 模板时仅引用附件....如果可能的话。

回答by RealHowTo

You add the image as an attachment and then you make a reference to it with a "cid" prefix.

您将图像添加为附件,然后使用“cid”前缀对其进行引用。

//
// This HTML mail have to 2 part, the BODY and the embedded image
//
MimeMultipart multipart = new MimeMultipart("related");

// first part  (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><img src=\"cid:[email protected]\">";
messageBodyPart.setContent(htmlText, "text/html");

// add it
multipart.addBodyPart(messageBodyPart);

// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource
  ("C:\images\foo.gif");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<[email protected]>");

// add it
multipart.addBodyPart(messageBodyPart);

// put everything together
message.setContent(multipart);

Complete example here

完整的例子在这里

回答by objects

Try the following which uses a ByteArrayDataSource to include your image bytes in the mail

尝试以下使用 ByteArrayDataSource 在邮件中包含图像字节的操作

// Add html content
// Specify the cid of the image to include in the email

String html = "<html><body><b>Test</b> email <img src='cid:my-image-id'></body></html>";
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, "text/html");
mp.addBodyPart(htmlPart);

// add image in another part

MimeBodyPart imagePart = new MimeBodyPart();
DataSource fds = new ByteArrayDataSource(imageBytes, imageType);
imagePart.setDataHandler(new DataHandler(fds));

// assign a cid to the image

imagePart.setHeader("Content-ID", "<my-image-id>"); // Make sure you use brackets < >
mp.addBodyPart(imagePart);

message.setContent(mp);

Adapted from example @ http://helpdesk.objects.com.au/java/how-to-embed-images-in-html-mail-using-javamail

改编自示例@ http://helpdesk.objects.com.au/java/how-to-embed-images-in-html-mail-using-javamail