java - 如何使用 html 模板在 java 电子邮件中添加图像
问题描述
我正在尝试发送java
带有图像的电子邮件,但是,我无法这样做,有人可以告诉我,为什么它不起作用,我正在发送电子邮件html template
。我究竟做错了什么?
SendEmail.java
文件code
如下。
` //Email settings code here
MimeMultipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(FROM,FROMNAME));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
String image ="<img src=\"cid:image\">";
msg.setSubject(SUBJECT);
DataSource fds = new FileDataSource("C:\\images\\web_logo_white.0e72366a.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
Map<String, String> input = new HashMap<String, String>();
if (msgBody.contains("Summary page was not")) {
input.put("logo", image);
}else{
// else block and other code continues here }
HTML file
我想在哪里添加图片
<table bgcolor="#f9f9f9" width="100%">
<tr>
<td>logo<h2><font color="#9d9d9d" ><b><center>Company Logo<center></b></font></h2>
</td>
</tr>
</table>
我html
在 setContent 部分发送这个文件java
解决方案
像这样流式传输图像
InputStream imageStream = YOurClass.class.getClass().getResourceAsStream("images/web_logo_white.0e72366a.png");
DataSource fds = new ByteArrayDataSource(IOUtils.toByteArray(imageStream), "image/png");
messageBodyPart.setDataHandler(new DataHandler(fds));
multipart.addBodyPart(messageBodyPart);
推荐阅读
- php - 有没有办法在 php 的序列化字符串中指定“反序列化函数”?
- node.js - 如何将 Ava 测试拆分为多个文件?
- visual-studio-code - Visual Studio Code:从搜索中排除路径
- go - 如何在官方 neo4j go 驱动程序中解析结果?
- python - 获取 usr/bin/python2.7:没有这样的文件或目录
- php - 如何在wordpress中将新的用户元键插入数据库
- python - 特定 ISO 格式的 Pandas 日期时间转换
- python-2.7 - 如何使用ascii将字符更改为之前的字符
- c - 从函数中获取随机字符
- debugging - 程序集:如何生成 .section .debug_info?