简体   繁体   English

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

[英]Insert Image Directly into an HTML Template Using Javamail

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. 基本上当我发送我的Javamail消息时,我将我的图像作为byte []对象,我将有一个包含html模板的字符串。 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. 我想把我已经拥有的byte []对象直接存储在html模板中,确保它在正确的图像标签中。 Is there a way I could do this? 有没有办法可以做到这一点? Basically I want to stick a message.setContent("blah","image/jpg"); 基本上我想坚持一个message.setContent(“blah”,“image / jpg”); directly into the html template at a specific spot. 直接进入特定位置的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模板时引用附件....如果可行的话。

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:image@foo.com\">";
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","<image@foo.com>");

// add it
multipart.addBodyPart(messageBodyPart);

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

Complete example here 在这里完成示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM