Springboot发送邮件工具类,可带附件,可以模版形式发送

简介: Springboot发送邮件工具类,可带附件,可以模版形式发送

发送邮件

  • pom.xml添加引用
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 支持发送邮件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>1.5.9.RELEASE</version>
</dependency>
  • 配置发送邮件的application


spring
  mail:
    default-encoding: UTF-8
    host: smtp.qq.com
    username: 1150640979@qq.com
    password: 1234567890
    port: 22
    protocol: smtp
    properties:
      mail:
        smtp:
          ssl:
            enable: true
          socketFactory:
            port: 465
            class: javax.net.ssl.SSLSocketFactory
  • 编写邮件的工具类
@Service
public class EmailUtil {
    @Autowired
    private JavaMailSender mailSender;
    /**
     * 用来发送模版邮件
     */
    @Autowired
    private TemplateEngine templateEngine;
    @Value("${spring.mail.username}")
    private String from;
    @Async
    public void sendEmail(Context context, String templateName, String to, String [] cc,
                          String subject, String text, List<String> attachmentList){
        try {
          // 解决附件名称过长导致的附件名称乱码问题
            System.setProperty("mail.mime.splitlongparameters", "false");
            // 定义邮件信息
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper;
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            if(cc != null && cc.length > 0){
                helper.setCc(cc);
            }
            // 如果存在模板,定义邮件模板中的内容,context的内容对应email.html的${project}占位的内容
            if(context != null && StringUtils.isNotBlank(templateName)){
                String emailContent = templateEngine.process(templateName, context);
                helper.setText(emailContent, true);
            }else{
                helper.setText(text);
            }
            // 如果存在附件,定义邮件的附件
            if(attachmentList != null && attachmentList.size() > 0){
                for (String attachment : attachmentList) {
                    FileSystemResource file = new FileSystemResource(attachment);
                    helper.addAttachment(file.getFilename(), file);
                }
            }
            mailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
  • 配置发送邮件的模板
    在resources目录下创建templates目录。创建email.html模板。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>yimcarson</title>
    <style>
        body {
            text-align: center;
            margin-left: auto;
            margin-right: auto;
        }
        #main {
            text-align: center;
        }
    </style>
</head>
<body>
<div id="main">
    <h3>Welcome <span th:text="${project}"></span> -By <span th:text=" ${author}"></span></h3>
    Your Verification Code is
    <h2><span th:text="${code}"></span></h2>
</div>
</body>
</html>


目录
相关文章
|
3月前
|
安全 JavaScript Java
SpringBoot实现定时发送邮件
SpringBoot实现定时发送邮件
50 0
|
3月前
|
Java Spring
Spring boot项目如何发送邮件
Spring boot项目如何发送邮件
34 2
|
3月前
|
数据采集 移动开发 前端开发
springboot使用html模版导出pdf文档
springboot使用html模版导出pdf文档
|
3月前
|
Java
Springboot如何发送邮件
Springboot如何发送邮件
|
3月前
|
Java API 开发工具
SpringBoot助力!轻松实现微信模版消息推送
SpringBoot助力!轻松实现微信模版消息推送
|
3月前
|
消息中间件 JSON Java
如何利用springboot + rabbitmq发送邮件?
RabbitMQ相关知识请参考: RabbitMQ消息确认、消息持久化等核心知识总结 - 简书
41 2
|
3月前
|
Java
【Java专题_03】使用springboot发送邮件
【Java专题_03】使用springboot发送邮件
45 2
|
3月前
|
安全 Java 数据安全/隐私保护
SpringBoot+Email发送邮件
邮件通知是现代应用中常见的一种通信方式,特别是在需要及时反馈、告警或重要事件通知的场景下。Spring Boot提供了简单而强大的邮件发送功能,使得实现邮件通知变得轻而易举。本文将研究如何在Spring Boot中使用JavaMailSender实现邮件发送,以构建一个可靠的通知体系。
174 1
SpringBoot+Email发送邮件
|
8月前
|
Java
SpringBoot项目发送邮件
SpringBoot项目发送邮件
67 1
|
9月前
|
Java 测试技术 Maven
Spring Boot发送邮件
Spring Boot发送邮件