Reputation: 33
I am trying create Email Template in spring + velocity .I am getting Email from this and content from template as well but the html in my template of velocity is not rendered on mail body. Here is my java function of sending email template
public void sendMail(String to, String subject, String body)
{
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(this.userName);
message.setTo(to);
message.setSubject(subject);
message.setText(body);
Template template = velocityEngine.getTemplate("Templates/EmailTemplate.vm");
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("firstName", "Yashwant");
velocityContext.put("lastName", "Chavan");
velocityContext.put("location", "Pune");
StringWriter stringWriter = new StringWriter();
template.merge(velocityContext, stringWriter);
message.setText(stringWriter.toString());
mailSender.send(message);
}
and my velocity Template is
<html>
<body>
<b>Dear ${firstName} ${lastName}</b>
Sending Email Using Spring Smtp Email + Velocity Template from ${location} location.
Thanks
</body>
</html>
Upvotes: 0
Views: 5973
Reputation: 5492
It seems SimpleMailMessage
only sends plain-text e-mails so you cannot use html with it.
Take a look at examples in Spring documentation that use MimeMessageHelper
. MimeMessageHelper
has method setText
that has second boolean parameter with which you can specify if the text is html (e.g. passing in true
means text is html). Take a look at the following example in the docs: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mail.html#mail-usage-mime
(There's also Velocity based example further down in the docs).
Upvotes: 3