重学SpringBoot3之如何发送Email邮件功能

 更新时间:2024年11月22日 09:23:44   作者:CoderJia_  
这篇文章主要给大家介绍了重学SpringBoot3之如何发送Email邮件功能的相关资料,文中包括环境准备、项目配置、代码实现、最佳实践和安全性建议,通过采用异步发送、重试机制、限流等最佳实践,可以构建一个健壮的邮件发送系统,需要的朋友可以参考下

前言

在企业应用开发中,发送邮件是一个非常常见的需求,比如用户注册验证、密码重置、系统通知等场景。SpringBoot 3提供了完善的邮件发送支持,本文将详细介绍如何在SpringBoot 3中实现邮件发送功能,并提供最佳实践建议。

1. 环境准备

  • JDK 17+
  • SpringBoot 3.0+
  • Maven/Gradle

2. 项目配置

2.1 添加依赖

在 pom.xml中添加以下依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.2 配置邮件服务器

在 application.yml中添加邮件服务器配置:

spring:
  mail:
    host: smtp.qq.com  # QQ邮件服务器地址
    port: 587            # 端口号
    username: ${username}
    password: ${password}  # 需要去QQ邮箱开通开通POP3/IMAP服务,并设置专用密码
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
  thymeleaf:
    cache: true
    check-template: true
    check-template-location: true
    content-type: text/html
    enabled: true
    encoding: UTF-8
    excluded-view-names: ''
    mode: HTML
    prefix: classpath:/templates/
    suffix: .html

3. 代码实现

3.1 创建邮件服务接口

package com.example.springboot3email.service;

import org.springframework.web.multipart.MultipartFile;

/**
 * @author CoderJia
 * @create 2024/11/21 10:22
 * @Description
 **/
public interface IEmailService {

    void sendSimpleEmail(String to, String subject, String text);

    void sendHtmlEmail(String to, String subject, String htmlContent);

    void sendEmailWithAttachment(String to, String subject, String text, MultipartFile attachment);
}

3.2 实现邮件服务

package com.example.springboot3email.service;

import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
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.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author CoderJia
 * @create 2024/11/21 10:23
 * @Description
 **/
@Service
@Slf4j
public class EmailServiceImpl implements IEmailService {

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送简单文本邮件
     *
     * @param to
     * @param subject
     * @param text
     */
    @Override
    public void sendSimpleEmail(String to, String subject, String text) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);

            mailSender.send(message);
            log.info("Simple email sent successfully to: {}", to);
        } catch (Exception e) {
            log.error("Failed to send simple email", e);
            throw new RuntimeException("Failed to send email", e);
        }
    }

    /**
     * 发送HTML格式的邮件
     *
     * @param to
     * @param subject
     * @param htmlContent
     */
    @Override
    public void sendHtmlEmail(String to, String subject, String htmlContent) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(htmlContent, true);

            mailSender.send(message);
            log.info("HTML email sent successfully to: {}", to);
        } catch (Exception e) {
            log.error("Failed to send HTML email", e);
            throw new RuntimeException("Failed to send email", e);
        }
    }

    /**
     * 发送带附件的邮件
     *
     * @param to
     * @param subject
     * @param text
     * @param attachment
     */
    @Override
    public void sendEmailWithAttachment(String to, String subject, String text, MultipartFile attachment) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text);

            // 创建临时文件
            File tempFile = File.createTempFile("upload_", attachment.getOriginalFilename());
            attachment.transferTo(tempFile);

            // 使用 FileSystemResource 包装临时文件
            FileSystemResource file = new FileSystemResource(tempFile);
            helper.addAttachment(attachment.getOriginalFilename(), file);

            mailSender.send(message);
            log.info("Email with attachment sent successfully to: {}", to);

            // 删除临时文件
            tempFile.delete();
        } catch (Exception e) {
            log.error("Failed to send email with attachment", e);
            throw new RuntimeException("Failed to send email", e);
        }
    }
}

3.3 创建邮件模板

在 resources/templates目录下创建HTML模板 EmailTemplate.html(使用Thymeleaf):

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Email Template</title>
</head>
<body>
    <h1>Welcome, <span th:text="${name}">User</span>!</h1>
    <p>This is a sample email template using Thymeleaf.</p>
    <p>Your verification code is: <strong th:text="$[code]">123456</strong></p>
</body>
</html>

3.4 使用模板发送邮件

@Service
public class TemplateEmailService {
  
    @Autowired
    private EmailService emailService;
  
    @Autowired
    private TemplateEngine templateEngine;
  
    public void sendTemplateEmail(String to, String subject, String templateName, Map<String, Object> variables) {
        Context context = new Context();
        variables.forEach(context::setVariable);
      
        String htmlContent = templateEngine.process(templateName, context);
        emailService.sendHtmlEmail(to, subject, htmlContent);
    }
}

4. 最佳实践建议

4.1 异步发送

为避免邮件发送影响主业务流程,建议使用异步方式发送邮件:

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
  
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
        executor.setThreadNamePrefix("EmailAsync-");
        executor.initialize();
        return executor;
    }
}

@Service
public class AsyncEmailService {
  
    @Async
    public CompletableFuture<Void> sendEmailAsync(String to, String subject, String content) {
        // 邮件发送逻辑

        return CompletableFuture.completedFuture(null);
    }
}

4.2 重试机制

实现邮件发送重试机制:

@Service
public class RetryableEmailService {
  
    @Retryable(value = {MessagingException.class}, 
               maxAttempts = 3,
               backoff = @Backoff(delay = 1000))
    public void sendEmailWithRetry(String to, String subject, String content) {
        // 邮件发送逻辑
    }
  
    @Recover
    public void recover(MessagingException e, String to) {
        // 处理最终失败的情况
        log.error("Failed to send email after retries", e);
    }
}

4.3 邮件发送限流

使用令牌桶算法实现邮件发送限流:

@Service
public class RateLimitedEmailService {
  
    private final RateLimiter rateLimiter = RateLimiter.create(10.0); // 每秒10封邮件
  
    public void sendEmailWithRateLimit(String to, String subject, String content) {
        if (rateLimiter.tryAcquire()) {
            // 发送邮件
        } else {
            throw new RuntimeException("Rate limit exceeded");
        }
    }
}

4.4 邮件发送记录

记录邮件发送历史:

@Entity
@Table(name = "email_log")
public class EmailLog {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
  
    private String to;
    private String subject;
    private LocalDateTime sendTime;
    private EmailStatus status;
    private String errorMessage;
  
    // getters and setters
}

5. 安全性建议

  • 不要在代码中硬编码邮件服务器凭证
  • 使用环境变量或配置中心存储敏感信息
  • 实现邮件发送频率限制,防止滥用
  • 记录邮件发送日志,便于问题排查
  • 使用TLS/SSL加密传输
  • 定期轮换邮件服务器密码

6. 测试示例

package com.example.springboot3email.controller;

import com.example.springboot3email.service.IEmailService;
import com.example.springboot3email.service.TemplateEmailService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author CoderJia
 * @create 2024/11/21 10:32
 * @Description
 **/
@Slf4j
@RestController
public class EmailController {
    @Resource
    private IEmailService emailService;

    @Resource
    private TemplateEmailService templateEmailService;


    // 发送简单文本邮件接口
    @PostMapping("/sendSimpleEmail")
    public String sendSimpleEmail(@RequestParam("to") String to,
                                  @RequestParam("subject") String subject,
                                  @RequestParam("text") String text) {
        emailService.sendSimpleEmail(to, subject, text);
        return "success";
    }

    // 发送HTML邮件接口
    @PostMapping("/sendHtmlEmail")
    public String sendHtmlEmail(@RequestParam("to") String to,
                                @RequestParam("subject") String subject,
                                @RequestParam("htmlContent") String htmlContent) {
        emailService.sendHtmlEmail(to, subject, htmlContent);
        return "success";
    }

    // 发送带附件的邮件接口
    @PostMapping("/sendEmailWithAttachment")
    public String sendEmailWithAttachment(@RequestParam("to") String to,
                                          @RequestParam("subject") String subject,
                                          @RequestParam("text") String text,
                                          @RequestParam("attachment") MultipartFile attachment) {
        emailService.sendEmailWithAttachment(to, subject, text, attachment);
        return "success";
    }


    // 发送模板邮件接口
    @PostMapping("/sendTemplateEmail")
    public String sendTemplateEmail(@RequestParam("to") String to,
                                    @RequestParam("subject") String subject,
                                    @RequestParam("templateName") String templateName) {
        Map<String, Object> variables = new HashMap<>();
        variables.put("name", "CoderJia");
        variables.put("code", 12530);
        templateEmailService.sendTemplateEmail(to, subject, templateName, variables);
        return "success";
    }
}

发送效果

普通邮件

HTML邮件

带附件邮件

模板邮件

7. 常见问题解决

7.1 连接超时

spring:
  mail:
    properties:
      mail:
        smtp:
          connectiontimeout: 5000
          timeout: 5000
          writetimeout: 5000

7.2 SSL证书问题

spring:
  mail:
    properties:
      mail:
        smtp:
          ssl:
            trust: "*"

7.3 中文乱码

helper.setFrom(new InternetAddress(from, "发件人名称", "UTF-8"));

8. 总结

本文详细介绍了在SpringBoot 3中实现邮件发送功能的完整解决方案,包括基本配置、代码实现、最佳实践、安全建议等内容。通过采用异步发送、重试机制、限流等最佳实践,可以构建一个健壮的邮件发送系统。在实际应用中,要根据具体需求选择合适的实现方式,同时注意安全性和性能的平衡。

到此这篇关于重学SpringBoot3之如何发送Email邮件功能的文章就介绍到这了,更多相关SpringBoot3发送Email内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 大厂禁止SpringBoot在项目使用Tomcat容器原理解析

    大厂禁止SpringBoot在项目使用Tomcat容器原理解析

    这篇文章主要为大家介绍了大厂禁止SpringBoot在项目使用Tomcat原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • SpringBoot RestTemplate 简单包装解析

    SpringBoot RestTemplate 简单包装解析

    这篇文章主要介绍了SpringBoot RestTemplate 简单包装解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • MyBatis-Plus Page 分页不生效的问题解决

    MyBatis-Plus Page 分页不生效的问题解决

    分页是常见的一种功能,本文主要介绍了MyBatis-Plus Page分页不生效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • 解决IDEA 左侧Project中没有out文件夹的问题

    解决IDEA 左侧Project中没有out文件夹的问题

    这篇文章主要介绍了解决IDEA 左侧Project中没有out文件夹的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Springboot中集成Swagger2框架的方法

    Springboot中集成Swagger2框架的方法

    这篇文章主要介绍了Springboot中集成Swagger2框架的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-12-12
  • SpringBoot添加License的多种方式

    SpringBoot添加License的多种方式

    License指的是版权许可证,当我们开发完系统后,如果不想让用户一直白嫖使用,比如说按时间续费,License的作用就有了。我们可以给系统指定License的有效期,控制系统的可用时间。
    2021-06-06
  • SSH框架网上商城项目第4战之EasyUI菜单的实现

    SSH框架网上商城项目第4战之EasyUI菜单的实现

    SSH框架网上商城项目第4战之EasyUI菜单的实现,本文主要使用EasyUI技术简单实现后台菜单,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • idea安装与配置及基本用法教程详解

    idea安装与配置及基本用法教程详解

    这篇文章主要介绍了idea安装与配置及基本用法教程详解,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • Java输入输出流实例详解

    Java输入输出流实例详解

    这篇文章主要介绍了Java输入输出流,结合实例形式详细分析了Java常见的输入输出常用操作技巧与相关注意事项,需要的朋友可以参考下
    2018-09-09
  • Java从源码角度解析SpringMVC执行流程

    Java从源码角度解析SpringMVC执行流程

    这篇文章主要介绍了Java从源码角度解析SpringMVC执行流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04

最新评论