public class AnotherExampleBean implements DisposableBean {
public void destroy() {
// do some destruction work (like releasing pooled connections)
}
}
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();
}
}
}
/** * 获取过去的天数 * @param date * @return */ public static long pastDays(Date date) { long t = new Date().getTime()-date.getTime(); return t/(24*60*60*1000); }
/** * 获取过去的小时 * @param date * @return */ public static long pastHour(Date date) { long t = new Date().getTime()-date.getTime(); return t/(60*60*1000); }
/** * 获取过去的分钟 * @param date * @return */ public static long pastMinutes(Date date) { long t = new Date().getTime()-date.getTime(); return t/(60*1000); }
/** * 转换为时间(天,时:分:秒.毫秒) * @param timeMillis * @return */ public static String formatDateTime(long timeMillis){ long day = timeMillis/(24*60*60*1000); long hour = (timeMillis/(60*60*1000)-day*24); long min = ((timeMillis/(60*1000))-day*24*60-hour*60); long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60); long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000); return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss; }
/** * 获取两个日期之间的天数 * * @param before * @param after * @return */ public static double getDistanceOfTwoDate(Date before, Date after) { long beforeTime = before.getTime(); long afterTime = after.getTime(); return (afterTime - beforeTime) / (1000 * 60 * 60 * 24); }
/** * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { // System.out.println(formatDate(parseDate("2010/3/6"))); // System.out.println(getDate("yyyy年MM月dd日 E")); // long time = new Date().getTime()-parseDate("2012-11-19").getTime(); // System.out.println(time/(24*60*60*1000)); } }