如何在使用 Java 邮件发送 html 电子邮件时嵌入多个图像
我正在尝试从 javamail 发送电子邮件。我正在嵌入图像并使用 CID。但问题是如何在一条消息中嵌入多个图像。如果我尝试添加标头..它只是采用最后设置的标头。如何使用 CID 添加多个图像和参考。
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
// BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><br/> <p align=center><img src=\"cid:senny\"> </p>";
htmlText+="<p align=center><img src=\"cid:senny\"> </p>";
htmlText+="<p align=center><img src=\"cid:image\"> </p>";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource
("C:\\images\\cec_header_457.png");
DataSource fds1 = new FileDataSource
("C:\\images\\cec_header_420.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setDataHandler(new DataHandler(fds1));
messageBodyPart.addHeader("Content-ID","<image>");
messageBodyPart.addHeader("Content-ID","<senny>");
// add it
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
I'm trying to send email from javamail. I'm embeding the images and using CID. But the problem is how do I embed multiple images in a single message. if I try to add in header.. it is just taking the last set header. how do I add multiple images and reference using CID.
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
// BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><br/> <p align=center><img src=\"cid:senny\"> </p>";
htmlText+="<p align=center><img src=\"cid:senny\"> </p>";
htmlText+="<p align=center><img src=\"cid:image\"> </p>";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource
("C:\\images\\cec_header_457.png");
DataSource fds1 = new FileDataSource
("C:\\images\\cec_header_420.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setDataHandler(new DataHandler(fds1));
messageBodyPart.addHeader("Content-ID","<image>");
messageBodyPart.addHeader("Content-ID","<senny>");
// add it
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个图像都需要有自己的 MimeBodyPart,将这段代码分解
为两个多部分,例如
Each image needs to be its own MimeBodyPart, break up this code,
Into two multi parts, something like
是的,每个图像都需要自己的 MimeBodyPart。上述解决方案仅适用于两张图像。如果有多个图像,为 MimeBodyPart 创建多个对象效率不高。
那么,我们就上函数吧,
这里要传递的参数是MimeBodyPart对象和图像路径。
每个图像都需要自己的 content-id,因此 content-id 是针对此场景随机生成的。
嵌入多个图像的完整代码:
Yes each image needs its own MimeBodyPart. Above solution is only for two images. In case, if there are multiple images, it is not efficient to create multiple object for MimeBodyPart.
So,Lets go up with function,
Here parameters to be passed are Object of MimeBodyPart and path of image.
Each image needs its own content-id, so content-id is generated randomly for this scenario.
Full code to embed multiple images :