BAEL-3964 HTML emails with Thymeleaf and FreeMarker (#9119)
This commit is contained in:
@@ -1,26 +1,49 @@
|
||||
package com.baeldung.spring.mail;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.thymeleaf.context.Context;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
|
||||
/**
|
||||
* Created by Olga on 7/15/2016.
|
||||
*/
|
||||
@Component
|
||||
@Service("EmailService")
|
||||
public class EmailServiceImpl implements EmailService {
|
||||
|
||||
@Autowired
|
||||
public JavaMailSender emailSender;
|
||||
|
||||
@Autowired
|
||||
public SimpleMailMessage template;
|
||||
|
||||
@Autowired
|
||||
private SpringTemplateEngine thymeleafTemplateEngine;
|
||||
|
||||
@Autowired
|
||||
private FreeMarkerConfigurer freemarkerConfigurer;
|
||||
|
||||
@Value("classpath:/mail-logo.png")
|
||||
Resource resourceFile;
|
||||
|
||||
public void sendSimpleMessage(String to, String subject, String text) {
|
||||
try {
|
||||
@@ -38,9 +61,8 @@ public class EmailServiceImpl implements EmailService {
|
||||
@Override
|
||||
public void sendSimpleMessageUsingTemplate(String to,
|
||||
String subject,
|
||||
SimpleMailMessage template,
|
||||
String ...templateArgs) {
|
||||
String text = String.format(template.getText(), templateArgs);
|
||||
String ...templateModel) {
|
||||
String text = String.format(template.getText(), templateModel);
|
||||
sendSimpleMessage(to, subject, text);
|
||||
}
|
||||
|
||||
@@ -66,4 +88,42 @@ public class EmailServiceImpl implements EmailService {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void sendMessageUsingThymeleafTemplate(
|
||||
String to, String subject, Map<String, Object> templateModel)
|
||||
throws MessagingException {
|
||||
|
||||
Context thymeleafContext = new Context();
|
||||
thymeleafContext.setVariables(templateModel);
|
||||
|
||||
String htmlBody = thymeleafTemplateEngine.process("template-thymeleaf.html", thymeleafContext);
|
||||
|
||||
sendHtmlMessage(to, subject, htmlBody);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessageUsingFreemarkerTemplate(
|
||||
String to, String subject, Map<String, Object> templateModel)
|
||||
throws IOException, TemplateException, MessagingException {
|
||||
|
||||
Template freemarkerTemplate = freemarkerConfigurer.createConfiguration().getTemplate("template-freemarker.ftl");
|
||||
String htmlBody = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerTemplate, templateModel);
|
||||
|
||||
sendHtmlMessage(to, subject, htmlBody);
|
||||
}
|
||||
|
||||
private void sendHtmlMessage(String to, String subject, String htmlBody) throws MessagingException {
|
||||
|
||||
MimeMessage message = emailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||
helper.setTo(to);
|
||||
helper.setSubject(subject);
|
||||
helper.setText(htmlBody, true);
|
||||
helper.addInline("attachment.png", resourceFile);
|
||||
emailSender.send(message);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user