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>
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 (eg 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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.