springboot mail+Thymeleaf模板

简介: compile 'org.springframework.boot:spring-boot-starter-thymeleaf' compile 'io.ratpack:ratpack-thymeleaf:1.

 

compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile 'io.ratpack:ratpack-thymeleaf:1.4.2'

 

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import ratpack.thymeleaf.internal.ThymeleafHttpServletRequestAdapter;
import ratpack.thymeleaf.internal.ThymeleafHttpServletResponseAdapter;
import ratpack.thymeleaf.internal.ThymeleafServletContextAdapter;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * Created by hujunzheng on 17/9/8.
 */
@Service
public class MailService {

    @Autowired
    private MailProperties mailProperties;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private TemplateEngine templateEngine;

    private static final Log log = LogFactory.getLog(MailService.class);
    /**
     * 发送邮件
     * @param userEmail
     * @param templateId
     */
    public void sendMail(String userEmail, Map<String, Object> params, int templateId) {
        sendMailMultiParams(new String[] {userEmail}, params, templateId);
    }

    public void sendMail(String userEmail, Object obj, int templateId) {
        Map<String, Object> params = objectToMap(obj);
        sendMailMultiParams(new String[] {userEmail}, params, templateId);
    }

    public void sendMail(String userEmail, String  message, String subject) {
        sendSampleMail(new String[] {userEmail}, message, subject);
    }

    public void sendMail(String[] userEmails, Map<String, Object> params, int templateId) {
        sendMailMultiParams(userEmails, params, templateId);
    }

    public void sendMail(String[] userEmails, Object obj, int templateId) {
        Map<String, Object> params = objectToMap(obj);
        sendMailMultiParams(userEmails, params, templateId);
    }

    public void sendMail(String[] userEmails, String  message, String subject) {
        sendSampleMail(userEmails, message, subject);
    }

    private Map<String, Object> objectToMap(Object obj) {
        PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(obj.getClass());
        Map<String, Object> params = new HashMap<>();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Optional<Method> getter = Optional.ofNullable(property.getReadMethod());
            try {
                Object value = getter.isPresent() ? getter.get().invoke(obj) : null;
                params.put(key, value);
            } catch (ReflectiveOperationException e) {
                e.printStackTrace();
            }
        }
        return params;
    }

    /**
     * 异步 分别发送 发送邮件
    * */
    public List<String> asynSendMails(List<ApiPortalOperator> apiPortalOperators, Object bodyParams) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        List<FutureTask<String>> futureTasks = new ArrayList<>();
        for (ApiPortalOperator apiPortalOperator : apiPortalOperators) {
            FutureTask<String> futureTask = new FutureTask(() -> {
                try {
                    this.sendMail(apiPortalOperator.getEmail(), bodyParams, 1);
                } catch (Exception e) {
                    return "邮件to " + apiPortalOperator.getEmail() + " 发送异常:" + e.getMessage();
                }
                return "邮件to " + apiPortalOperator.getEmail() + " 发送成功";
            });
            futureTasks.add(futureTask);
            executorService.submit(futureTask);
        }

        List<String> result = new ArrayList<>();
        for (int i=0; i<futureTasks.size(); ++i) {
            FutureTask futureTask = futureTasks.get(i);
            try {
                result.add((String) futureTask.get());
            } catch (Exception e) {
                result.add("to " + apiPortalOperators.get(i).getEmail() + " 发送失败:" + e.getMessage());
            }
        }

        return result;
    }

    /**
     * 异步 多个收件人一起 发送邮件
     * */
    public String asynSendMail(List<ApiPortalOperator> apiPortalOperators, Object bodyParams) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        String[] userEmails = apiPortalOperators.stream().map(ApiPortalOperator::getEmail).toArray(String[]::new);
        FutureTask<String> futureTask = new FutureTask(() -> {
            try {

                this.sendMail(userEmails, bodyParams, 1);
            } catch (Exception e) {
                return "邮件to " + StringUtils.join(userEmails, ',') + " 发送异常:" + e.getMessage();
            }
            return "邮件to " + StringUtils.join(userEmails, ',') + " 发送成功";
        });
        executorService.submit(futureTask);
        try {
            return futureTask.get();
        } catch (Exception e) {
            e.printStackTrace();
            return "邮件to " + StringUtils.join(userEmails, ',') + " 发送异常:" + e.getMessage();
        }
    }

    private void sendSampleMail(String[] userEmail, String msg, String subject) {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "utf-8");
                message.setTo(userEmail);
                message.setFrom(new InternetAddress(mailProperties.getFrom()));
                message.setSubject(subject);
                message.setText(msg);
            }
        };
        try {
            this.javaMailSender.send(preparator);
        } catch (Exception ex) {
            log.error("Mailing Exception! user email is: {}", userEmail, ex);
        }
        log.info("Mail sent successfully, user email is: {}", userEmail);
    }

    private String buildTemplateMessage(String templateName, Map<String, Object> messages) {
        HttpServletRequest request = new ThymeleafHttpServletRequestAdapter();
        HttpServletResponse response = new ThymeleafHttpServletResponseAdapter();
        ServletContext servletContext = new ThymeleafServletContextAdapter();
        WebContext context = new WebContext(request, response, servletContext);
        context.setVariables(messages);
        return templateEngine.process(templateName, context);
    }

    /**
     * 当发邮件时有多个不定参数时
     * @param userEmail 用户邮箱
     * @param params 发邮件的参数
     * @param templateId
     */
    private void sendMailMultiParams(String[] userEmail, Map<String, Object> params, Integer templateId) {

        String templateName = MailTemplate.getTemplateName(templateId);

        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "utf-8");
                message.setTo(userEmail);
                message.setFrom(new InternetAddress(mailProperties.getFrom()));
                message.setSubject(MailTemplate.getTemplateSubject(templateId));

                String content = buildTemplateMessage(templateName, params);
                System.out.println(content);
                message.setText(content, true);
            }
        };
        try {
            this.javaMailSender.send(preparator);
        } catch (Exception ex) {
            log.error("Mailing Exception! user email is: {}, template params are: {}, template_id is: {}", userEmail, params.toString(), templateId, ex);
        }
        log.info("Mail sent successfully, user email is: {}, template params: {}, template_id is: {}", userEmail, params.toString(), templateId);
    }
}

 

Thymeleaf 中有 plain context (不支持对url参数的解析)和 web context(支持对url参数的解析)

We'll have to change our code to create a web context, instead of a plain context. Change will be here: 

参考:https://github.com/ratpack/ratpack/blob/master/ratpack-thymeleaf/src/main/java/ratpack/thymeleaf/Template.java#L69-69

The IWebContext interface seems coupled to the servlet API, but after quick look at the code I don't think this will be a problem. I think we can just provide null values for things like request/session etc. that we can't provide. If that turns out to be a problem… we can look at providing implementations of those Servlet API type in so far makes sense.

 

最后附上一份简单的邮件模板文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>邮件提醒</title>
</head>
<body style="margin: 0; padding: 0;">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="padding: 20px; font-size: 14px;">
    <tbody>
    <tr>
        <td><p style="margin: 0; margin-bottom: 20px;">Hi,</p></td>
    </tr>
    <tr>
        <td><p style="margin: 0; margin-bottom: 20px;">项目<span th:text="${project}"/>的API已设计完成,请到<a href="#" th:href="@{ ${apiUrl} }">API Portal</a>上review。</p></td>
    </tr>
    <tr th:each="link,status : ${links}">
        <td><p style="margin: 0;"><span th:text="${status.count}"/>. <span th:text="${link.method}"/>&nbsp;<span th:text="${link.path}"/>&nbsp;<span th:text="${link.description}"/>&nbsp;<a href="#" th:href="@{ ${link.url} }">点击查看</a></p></td>
    </tr>
    <tr>
        <td><p style="margin: 0; margin-top: 20px;">Thanks</p></td>
    </tr>
    <tr>
        <td><p style="margin: 0;"><span th:text="${user}"/></p></td>
    </tr>
    </tbody>
</table>
</body>
</html>

 

目录
相关文章
|
8月前
|
前端开发 Java
SpringBoot-6-模板Thymeleaf常用标签
SpringBoot-6-模板Thymeleaf常用标签
43 0
SpringBoot-6-模板Thymeleaf常用标签
|
3月前
|
Java
SpringBoot thymeleaf自定义错误页面
SpringBoot thymeleaf自定义错误页面
19 0
|
10月前
|
Java Maven
springboot使用freemarker的入门Demo
1.创建一个maven web项目 2.添加所需要的依赖
|
10月前
|
前端开发 Java 开发者
【SpringBoot学习笔记 九】SpringBoot定制整合Thymeleaf及页面国际化(下)
【SpringBoot学习笔记 九】SpringBoot定制整合Thymeleaf及页面国际化(下)
103 0
|
10月前
|
移动开发 前端开发 JavaScript
【SpringBoot学习笔记 九】SpringBoot定制整合Thymeleaf及页面国际化
【SpringBoot学习笔记 九】SpringBoot定制整合Thymeleaf及页面国际化
61 0
|
安全 Java 数据安全/隐私保护
SpringBoot 整合 Mail|学习笔记
快速学习 SpringBoot 整合 Mail
86 0
SpringBoot 整合 Mail|学习笔记
|
消息中间件 JSON 前端开发
SpringBoot2.x系列教程08--SpringBoot中整合Thymeleaf动态模板
前言 在前面的章节中,壹哥 带各位利用SpringBoot实现了SSM整合,发现现在SSM整合变得的非常简单,通过几个配置就能搭建出一个项目开发环境。但是在之前的案例中,我们并没有提供UI界面,那么在SpringBoot中如何整合UI界面呢?使用JSP展示页面,还是用HTML展示页面,或者还有其他方案? 在今天的内容里,壹哥 会带大家学习如何在SpringBoot中展示UI界面,这样大家以后就可以把数据信息在页面上渲染展示了。 一. Web开发方式简介 SpringBoot作为一个简化项目开发的利器,其实它为我们提供了一套完整的Web开发方案,从前端到后端,再到数据库、定时任务、消息队列等都
226 0
|
前端开发 Java API
SpringBoot学习---thymeleaf模板引擎
SpringBoot学习---thymeleaf模板引擎
SpringBoot学习---thymeleaf模板引擎
|
Java Android开发
SpringBoot入门:SpringBoot整合Freemarker和Thymeleaf模板
关于springboot项目的创建可以看下面这篇文章,这里不进行叙述,可以参考之前的文章SpringBoot入门:使用IDEA和Eclipse构建第一个SpringBoot项目。
142 0
SpringBoot入门:SpringBoot整合Freemarker和Thymeleaf模板
|
XML 移动开发 缓存
SpringBoot中使用Thymeleaf模板
SpringBoot中使用Thymeleaf模板
158 0
SpringBoot中使用Thymeleaf模板