package xyz.suancaiyu.service.thymeleaf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import xyz.suancaiyu.Constat;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.Date;
/**
* 使用Thymeleaf发送模板邮件.
*
* Created by puruidong on 2017/9/15.
*/
@Component
public class EmailService {
@Autowired
private JavaMailSenderImpl mailSender;
@Autowired
private TemplateEngine emailTemplateEngine;
private static final String EMAIL_TEXT_TEMPLATE_NAME = "text/email-text";
private static final String EMAIL_SIMPLE_TEMPLATE_NAME = "html/email-simple";
private static final String EMAIL_WITHATTACHMENT_TEMPLATE_NAME = "html/email-withattachment";
private static final String EMAIL_INLINEIMAGE_TEMPLATE_NAME = "html/email-inlineimage";
private static final String EMAIL_EDITABLE_TEMPLATE_CLASSPATH_RES = "html/email-editable";
public void sendTextMail() {
// Prepare the evaluation context
final Context ctx = new Context();
ctx.setVariable("name", "test123");
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
// Prepare message using a Spring helper
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
try {
message.setSubject("Example plain TEXT email");
message.setFrom(Constat.getValue(Constat.SMTPFROMEMAIL));
message.setTo(Constat.getValue(Constat.SMTPTOEMAIL));
// Create the plain TEXT body using Thymeleaf
final String textContent = emailTemplateEngine.process(EMAIL_TEXT_TEMPLATE_NAME, ctx);
message.setText(textContent);
// Send email
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/*
* Send HTML mail (simple)
*/
public void sendSimpleMail() {
// Prepare the evaluation context
final Context ctx = new Context();
ctx.setVariable("name", "13343sdsxvbn");
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
// Prepare message using a Spring helper
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
try {
message.setSubject("Example HTML email (simple)");
message.setFrom(Constat.getValue(Constat.SMTPFROMEMAIL));
message.setTo(Constat.getValue(Constat.SMTPTOEMAIL));
// Create the HTML body using Thymeleaf
final String htmlContent = emailTemplateEngine.process(EMAIL_SIMPLE_TEMPLATE_NAME, ctx);
message.setText(htmlContent, true /* isHtml */);
// Send email
this.mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void sendMailWithInline() {
final Context ctx = new Context();
ctx.setVariable("name", "testlocal");
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
ctx.setVariable("imageResourceName", "identifier1234"); // so that we can reference it from HTML
final MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setSubject("Thymeleaf模板邮件");
helper.setFrom(Constat.getValue(Constat.SMTPFROMEMAIL));
helper.setTo(Constat.getValue(Constat.SMTPTOEMAIL));
final String htmlContent = emailTemplateEngine.process(EMAIL_INLINEIMAGE_TEMPLATE_NAME, ctx);
helper.setText(htmlContent, true);
helper.addInline("identifier1234", new ClassPathResource(Constat.THYMELEAF_LOGO_IMAGE), Constat.PNG_MIME);
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/*
* Send HTML mail with inline image
*/
public void sendEditableMail() {
try {
// Prepare message using a Spring helper
final MimeMessage mimeMessage = mailSender.createMimeMessage();
final MimeMessageHelper message
= new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8");
message.setSubject("Example editable HTML email");
message.setFrom(Constat.getValue(Constat.SMTPFROMEMAIL));
message.setTo(Constat.getValue(Constat.SMTPTOEMAIL));
// Prepare the evaluation context
final Context ctx = new Context();
ctx.setVariable("name", "fdsafdsadsaork11233");
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
// Create the HTML body using Thymeleaf
final String output = emailTemplateEngine.process(EMAIL_EDITABLE_TEMPLATE_CLASSPATH_RES, ctx);
message.setText(output, true /* isHtml */);
// Add the inline images, referenced from the HTML code as "cid:image-name"
message.addInline("background", new ClassPathResource(Constat.BACKGROUND_IMAGE), Constat.PNG_MIME);
message.addInline("logo-background", new ClassPathResource(Constat.LOGO_BACKGROUND_IMAGE), Constat.PNG_MIME);
message.addInline("thymeleaf-banner", new ClassPathResource(Constat.THYMELEAF_BANNER_IMAGE), Constat.PNG_MIME);
message.addInline("thymeleaf-logo", new ClassPathResource(Constat.THYMELEAF_LOGO_IMAGE), Constat.PNG_MIME);
// Send mail
mailSender.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}