How to add html image in to velocity template file to send email?

爷,独闯天下 提交于 2019-12-06 15:19:20

You can follow the guide here.

For example, try this in your Velocity template file:

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

And in your Java code, try:

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

When your server is running you can get path till server from request.getContexPath();

So here you just need to provide rest path of the image. I have done this for my demo application like this.

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

Now you have to set value of projectPath to your projectPath which you can get by request.getContexPath();

Now create one Map in which you have to add Key which will keyword that you have used in .vm file. For this example, we have used projectPath.

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

After that create instance of VelocityContext load this map with constructor argument like this

 VelocityContext velocityContext = new VelocityContext(map);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!