SpringBoot整合邮箱发送邮件

SpringBoot整合邮箱发送邮件

引入依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

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

配置文件
server.port=8082
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.password=[POP3/IMAP/SMTP/Exchange/CardDAV 服务 授权码]
spring.mail.username=843566121@qq.com
spring.mail.port=587
spring.mail.properties.mail.stmp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true

Service层接口及实现类
/**
 * <p>
 * 邮件发送Service层接口
 * </p>
 *
 * @author jpge
 * @since 2023-09-23
 */
public interface EmailService {
    /**
     * 发送邮箱验证码
     *
     * @param mailAddress 邮箱地址
     * @param code        验证码
     * @param sec         安全码
     */
    void sendSignUpCaptcha(String mailAddress, String code, Integer sec);
}
import com.edu.vertifycode.mail.service.EmailService;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * <p>
 * 邮件发送Service层接口 实现类
 * </p>
 *
 * @author jpge
 * @since 2023-09-23
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    JavaMailSender javaMailSender;

    @Resource
    MailProperties mailProperties;

    @Resource
    TemplateEngine templateEngine;

    /**
     * 发送邮箱验证码
     *
     * @param mailAddress 邮箱地址
     * @param code        验证码
     * @param sec         安全码
     */
    public void sendSignUpCaptcha(String mailAddress, String code, Integer sec) {
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg);
        try {
            //设置邮件元信息
            helper.setTo(mailAddress);
            helper.setFrom(mailProperties.getUsername());
            helper.setSubject("验证码");
            helper.setSentDate(new Date());
            //模板渲染
            Context context = new Context();
            context.setVariable("name", "HELLO_WORLD");
            context.setVariable("code", code);
            context.setVariable("sec", sec);
            String mail = templateEngine.process("mail", context);
            helper.setText(mail, true);
            javaMailSender.send(msg);
            System.out.println("邮件发送成功!");
        } catch (MessagingException e) {
            System.out.println("邮件发送失败" + e.getMessage());
        }
    }
}

邮件模板[templates/mail.html]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎注册 HELLO_WORLD 网站!</title>
</head>
<style>
    .big-font {
        font-size: 25px;
    }
    .warning {
        color: red;
        background-color: bisque;
        display: inline;
    }
</style>
<body>
<h3>亲爱的 [[${name}]],欢迎注册 HELLO_WORLD 网站!</h3>

<p>您的<b>注册验证码</b>是:<b class="big-font"> [[${code}]] </b></p>
<p>您的<b>识别码</b>是:<b class="big-font"> [[${sec}]] </b></p>
<p class="warning">如果您并没有注册 HELLO_WORLD 网站,请忽略该邮件!</p>

</body>
</html>

测试启动类及自测用例
import com.edu.vertifycode.mail.service.EmailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = VerificationCodeMailApplication.class)
public class VerificationCodeMailApplicationTests {

    @Resource
    private EmailService emailService;

    @Test
    public void contextLoads() {
        System.out.println("HELLO_WORLD!!!");
        emailService.sendSignUpCaptcha(
                "1836868464@qq.com",
                "433999",
                8848
        );
    }

}

自测效果截图

在这里插入图片描述