如何使用 JavaMail 将图像添加到 Freemarker 模板
How to add image to a Freemarker template with JavaMail
现在,在这个问题被标记为重复之前,
Add image to freemarker template for mail?
这个问题没有提供我的问题的解决方案。
所以我想在我附加到 Java 中的 Google Gmail API 程序的 Freemarker HTML 模板中添加图像作为背景。
这是 Freemarker 的代码:-
<style>
body{
background-image: url("cid:${image.jpg}");
}
.container {
position: absolute;
padding:25px;
margin:25px;
border:5px;
text-align: justify;
text-justify: inter-word;
font-family: "Times New Roman";
font-style: italic;
}
</style>
我使用 Google Gmail API 插入图片的方式是:-
Multipart multipart = new MimeMultipart();
MimeBodyPart bodyPart = new MimeBodyPart();
Template t = freemarkerTemlate.getTemplate("OneEmployee.ftl");
String mailContent = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
bodyPart.setContent(mailContent, "text/html");
multipart.addBodyPart(bodyPart);
DataSource fds = new FileDataSource(Mailer.class.getClassLoader().getResource("images/image1.jpg").getFile());
bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(fds));
bodyPart.setHeader("Content-ID", "<image.jpg>");
multipart.addBodyPart(bodyPart);
我的图像位于包含另一个图像目录的资源目录中
现在我已经尝试了几乎所有可能的代码变体,例如使用
<img src="">
在 ${} 中添加 cid: 并且不使用 "" 但它似乎在任何情况下都没有设置图像。
在某些情况下它会在不附加图像的情况下运行,在某些情况下它会运行但也会在控制台上抛出错误:-
Application failed at {image1.jpg}:- Missing or Null Argument
我使用的模型是:-
Map model = new HashMap();
model.put("emailNames", emailNames);
我没有向模型添加任何其他内容,因为这是除了我插入到 freemarker 模板中的图像之外唯一的其他内容
我有什么做错或遗漏的地方吗?
编辑 2
我还尝试通过将模型设置为键来插入图像
代码已附上参考
Map model = new HashMap();
model.put("emailNames", emailNames);
model.put("image.jpg", Mailer.class.getClassLoader().getResource("images/image1.jpg").getFile())
还是不行!
编辑 2
有趣的是,我注意到当我没有定义 cid
时,它确实将背景图像添加为附件,但是每当我添加 cid
语法时,它不会添加为附件,但它只是忽略邮件。
我在网上看到,根据教程,您没有传递要作为模型的一部分插入的图像,而是将其添加为定义图像的 content_id (cid) 的附件.
谢谢
所以我终于得到了解决方案的答案
我从资源中的图像文件夹加载图像的方式是错误的
File file = new ClassPathResource("image").getFile();
这是方法
当将它传递给 Freemarker 模板时,您只需要做 cid:imageURL
就可以了,它应该可以工作。
您还应该遵循@Bill Shannon 和@Mark Rotteveel 给出的评论
现在,在这个问题被标记为重复之前, Add image to freemarker template for mail?
这个问题没有提供我的问题的解决方案。 所以我想在我附加到 Java 中的 Google Gmail API 程序的 Freemarker HTML 模板中添加图像作为背景。
这是 Freemarker 的代码:-
<style>
body{
background-image: url("cid:${image.jpg}");
}
.container {
position: absolute;
padding:25px;
margin:25px;
border:5px;
text-align: justify;
text-justify: inter-word;
font-family: "Times New Roman";
font-style: italic;
}
</style>
我使用 Google Gmail API 插入图片的方式是:-
Multipart multipart = new MimeMultipart();
MimeBodyPart bodyPart = new MimeBodyPart();
Template t = freemarkerTemlate.getTemplate("OneEmployee.ftl");
String mailContent = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
bodyPart.setContent(mailContent, "text/html");
multipart.addBodyPart(bodyPart);
DataSource fds = new FileDataSource(Mailer.class.getClassLoader().getResource("images/image1.jpg").getFile());
bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(fds));
bodyPart.setHeader("Content-ID", "<image.jpg>");
multipart.addBodyPart(bodyPart);
我的图像位于包含另一个图像目录的资源目录中
现在我已经尝试了几乎所有可能的代码变体,例如使用
<img src="">
在 ${} 中添加 cid: 并且不使用 "" 但它似乎在任何情况下都没有设置图像。
在某些情况下它会在不附加图像的情况下运行,在某些情况下它会运行但也会在控制台上抛出错误:-
Application failed at {image1.jpg}:- Missing or Null Argument
我使用的模型是:-
Map model = new HashMap();
model.put("emailNames", emailNames);
我没有向模型添加任何其他内容,因为这是除了我插入到 freemarker 模板中的图像之外唯一的其他内容
我有什么做错或遗漏的地方吗?
编辑 2
我还尝试通过将模型设置为键来插入图像 代码已附上参考
Map model = new HashMap();
model.put("emailNames", emailNames);
model.put("image.jpg", Mailer.class.getClassLoader().getResource("images/image1.jpg").getFile())
还是不行!
编辑 2
有趣的是,我注意到当我没有定义 cid
时,它确实将背景图像添加为附件,但是每当我添加 cid
语法时,它不会添加为附件,但它只是忽略邮件。
我在网上看到,根据教程,您没有传递要作为模型的一部分插入的图像,而是将其添加为定义图像的 content_id (cid) 的附件.
谢谢
所以我终于得到了解决方案的答案
我从资源中的图像文件夹加载图像的方式是错误的
File file = new ClassPathResource("image").getFile();
这是方法
当将它传递给 Freemarker 模板时,您只需要做 cid:imageURL
就可以了,它应该可以工作。
您还应该遵循@Bill Shannon 和@Mark Rotteveel 给出的评论