Question

I am using Velocity Engine as template engine to send email. Email template is in .vm files and I set any variables that I want to set and send email using JavaMail api. For English, this is working all fine. But for Arabice, I don't get the letters correctly.

Note that I have set content type utf-8 in html format and also set content type in the Java code also.

<html>
 <head><meta charset="utf-8"></head>...

Also I don't have the luxory of using Spring, so VelocityEngineUtils cannot be used.

VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class",ClasspathResourceLoader.class.getName());
ve.init();

Template t = ve.getTemplate("email_template.vm");

VelocityContext context = new VelocityContext();
context.put("variable", "param");

StringWriter writer = new StringWriter();
t.merge(context, writer);

Following is the mail sending code. You can see that I set UTF-8 character encoding when setting text

MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress("senderEmail@gmail.com", "Test application"));
message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("receipant@gmail.com"));
message.setSubject("Testing Subject");
message.setText(writer.toString(),"UTF-8", "html");
Was it helpful?

Solution

Template t = ve.getTemplate("email_template.vm", "UTF-8"); 

did the trick

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top