Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

velocity

I include this in my velocity file. but it is not working,

< img src="cid:src/resources/imageContent.jpg" />
like image 563
Harshana Madusanka Jayamaha Avatar asked Jan 10 '17 07:01

Harshana Madusanka Jayamaha


2 Answers

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);
like image 86
kkmonlee Avatar answered Oct 13 '22 02:10

kkmonlee


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);
like image 24
Darshit Avatar answered Oct 13 '22 03:10

Darshit