【问题标题】:How to add html image in to velocity template file to send email?如何将 html 图像添加到速度模板文件中以发送电子邮件?
【发布时间】:2017-01-10 07:04:25
【问题描述】:

我将它包含在我的速度文件中。但它不起作用,

< img src="cid:src/resources/imageContent.jpg" />

【问题讨论】:

标签: java velocity


【解决方案1】:

您可以按照指南here

例如,在 Velocity 模板文件中试试这个:

<img src = "cid:${cid}" alt = "Foo">

在您的 Java 代码中,尝试:

URL url = new URL("image.png");
String cid = email.embed(url, "Foo");
Map model = new HashMap();
model.put("cid", cid);

【讨论】:

  • 如何将 JavaMail 与 Velocity 结合使用?
【解决方案2】:

当您的服务器运行时,您可以从request.getContexPath(); 获取到服务器的路径

所以这里你只需要提供图像的休息路径。我已经为我的演示应用程序这样做了。

 <img border="0" alt="Test" src="${projectPath}/images/logo.jpg"/></a></td> 

现在您必须将 projectPath 的值设置为您可以通过request.getContexPath(); 获取的 projectPath

现在创建一个地图,您必须在其中添加您在 .vm 文件中使用的关键字。对于这个例子,我们使用了 projectPath。

 Map map = new HashMap<>();
 map.add("projectPath",request.getContexPath());
 map.add() // other value that you want to replace in vm file

在创建 VelocityContext 实例之后,使用这样的构造函数参数加载此地图

 VelocityContext velocityContext = new VelocityContext(map);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 2021-03-30
    • 2020-08-16
    • 1970-01-01
    • 2015-01-02
    • 2019-06-11
    相关资源
    最近更新 更多