我们需要从java发送邮件,我需要使用freemarker.template.Configuration类加载一个Ftl tempalte,它有一个img src标签,我需要在classpath中添加一个图像 . 我尝试直接在img src标签中提供资源路径,但它不起作用 . 然后我找到了一个使用Base64编码包含图像的解决方案 . 它适用于雷鸟但不适用于webmail,outlook和gmail(这些邮件服务提供商不支持Base64嵌入式图像) . 我尝试在谷歌的一些其他方法,但没有得到一个正确的解决方案 . 如果我要使用javaMailSender,可以通过以下链接完成参考

但是对于我们的要求,我们需要将我们的邮件正文提供给其他一些会触发邮件的服务 . 所以在Short中如何在除了base64之外的java中将图像包含在ftl模板中 .

public class mailTrigger {

@Autowired

private Configuration freemarkerConfig;

private String frameMail(final String userName) throws Exception {

final Template t = freemarkerConfig.getTemplate("mailTemplate.ftl");

ClassPathResource res = new ClassPathResource("classpath:logo.jpg");

File img = new File(res.getPath());

byte[] imgBytes = IOUtils.toByteArray(new FileInputStream(img));

byte[] imgBytesAsBase64 = Base64.encodeBase64(imgBytes);

String imgDataAsBase64 = new String(imgBytesAsBase64);

String logo = "data:image/png;base64," + imgDataAsBase64;

final Map model = new HashMap();

model.put("userName", userName);

model.put("Logo", Logo);

final String message = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);

return message;

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐