发送邮件功能:使用Spring Email、邮件工具类、使用Thymeleaf模板引擎 发送html邮件

本文介绍如何使用Spring Boot发送文本及HTML格式的电子邮件,包括配置邮件服务、创建邮件发送工具类和进行邮件发送测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

发送邮件 Spring Email

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uyc8dHbX-1656836507364)(G:\笔记\笔记截图\image-20220703104617248.png)]

开启自己邮箱的POP3/SMTP服务

在这里插入图片描述

导入spring mail 依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>2.6.3</version>
</dependency>

邮箱参数配置 applicateion.properties

#MailProperties
spring.mail.host=smtp.sina.com //新浪   对应邮件服务厂家域名  QQ --- smtp.qq.com
spring.mail.port=465
spring.mail.username=xxxxxxxxxxxx
spring.mail.password=xxxxxxxxxxx //若出现连接错误,可替换为 认证码,去服务厂家获取
#ssl安全连接
spring.mail.properties.mail.smtp.ssl.enable=true

邮件工具类

/**
 * @Author 孑然
 *
 * 发送邮件类
 */
@Component
public class MailClient {
    public static final Logger logger = LoggerFactory.getLogger(MailClient.class);

    @Resource
    private JavaMailSender mailSender;

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

    /**
     * 发送邮件
     * @param to 发送目标
     * @param subject 邮件标题
     * @param content 邮件内容
     */
    public void sendMail(String to, String subject, String content) {
        try {
            // 构建邮件
            MimeMessage message = mailSender.createMimeMessage();

            //使用邮件助手构建邮件内容
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);//true代表可发送html格式

            //发送邮件
            mailSender.send(helper.getMimeMessage());

        } catch (MessagingException e) {
            logger.error("发送邮件失败" + e.getMessage());
        }
    }
}

测试发送邮件 文本邮件和html邮件

/**
 * @Author 孑然
 */
@SpringBootTest
public class MailTest {

    @Autowired
    private MailClient mailClient;

    /**
     * Thymeleaf模板引擎
     */
    @Resource
    private TemplateEngine templateEngine;

    @Test
    public void testTextMail(){
        mailClient.sendMail("xxxxxxx@qq.com", "TEST", "你好啊!");
    }

    @Test
    public void testHtmlMail() {
        //向html中传入数据
        Context context = new Context();
        context.setVariable("username", "sunday");

        //构建邮件内容
        String content = templateEngine.process("/mail/demo", context);
        System.out.println(content);

        //发送邮件
        mailClient.sendMail("xxxxxxx@qq.com", "html邮件", content);

    }

}

/static/mail/demo 目录下的 demo.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>邮件示例</title>
</head>
<body>
  <p>欢迎你,<span style="color: red" th:text="${username}"></span>!</p>
</body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值