如何使用JavaMail将图像添加到Freemarker模板
在这个问题被标记为重复之前, 这个问题不能解决我的问题。 所以我想在Freemarker HTML模板中添加图像作为背景,我将其附加到Java中的Google Gmail API程序 以下是Freemarker的代码:-如何使用JavaMail将图像添加到Freemarker模板,java,spring,spring-data,jakarta-mail,freemarker,Java,Spring,Spring Data,Jakarta Mail,Freemarker,在这个问题被标记为重复之前, 这个问题不能解决我的问题。 所以我想在Freemarker HTML模板中添加图像作为背景,我将其附加到Java中的Google Gmail API程序 以下是Freemarker的代码:- <style> body{ background-image: url("cid:${image.jpg}"); } .container { position: absolute; pad
<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>
我使用的模型是:-
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
语法时,它不会添加为附件,而只是忽略邮件
我在网上读到,根据教程,您不会传递要作为模型一部分插入的图像,而是将其作为定义图像内容id(cid)的附件添加
谢谢所以我终于找到了解决方案的答案 我从参考资料中的Images文件夹加载图像的方式是错误的
File file = new ClassPathResource("image").getFile();
这就是方法
当将其传递给Freemarker模板时,您只需执行cid:imageURL
即可,而且应该可以正常工作
此外,您还应遵循@Bill Shannon和@Mark Rotterveel给出的评论您能否分享
模型
以及您的设置方式?在模型中,您是否已将image1.jpg
设置为键?没有,先生,我尚未将image1设置为模型中的键,但我仍在附加模型。此部分是错误的。您可以在模型中设置图像路径
并发送到ftl您希望url(“cid:${image.jpg}”)
做什么?该错误表明,即使在您必须正确引用图像以使其实际工作之前,freemarker中的此操作也会出错。@MarkRotterVeel OP不会在模型上发送任何内容。显然,当您访问任何属性时,将为null。
Map model = new HashMap();
model.put("emailNames", emailNames);
Map model = new HashMap();
model.put("emailNames", emailNames);
model.put("image.jpg", Mailer.class.getClassLoader().getResource("images/image1.jpg").getFile())
File file = new ClassPathResource("image").getFile();