Spring发送邮箱功能

目录

一、邮箱设置

​二、Spring Email

1. 导入jar包

2. 邮箱参数配置

3. 使用JavaMailSender发送邮件(工具类)

 三、模板引擎


一、邮箱设置

  • 启用客户端SMTP服务

二、Spring Email

1. 导入jar包

在Maven Repository仓库中可以粘贴复制配置代码到pom.xml文件中:

Maven Repository: org.springframework.boot » spring-boot-starter-mail (mvnrepository.com)icon-default.png?t=LBL2https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail

2. 邮箱参数配置

在application.properties文件中写入以下配置参数: 

3. 使用JavaMailSender发送邮件(工具类)

构造一个发送邮件的工具类(MailClient),封装发送邮件的流程,以便复用:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component//需要Spring的容器来管理;且为通用的bean,在哪个层次都可以使用
public class MailClient {

    //声明logger,记录日志文件
    private static final Logger logger = LoggerFactory.getLogger(MailClient.class);

    //注入JavaMailSender对象
    @Autowired
    private JavaMailSender mailSender;

    //由properties配置文件中发送用户的键获取值
    @Value("${spring.mail.username}")
    private String from;

    public void sendMail(String to,String subject,String content){
        try {
            //1. JavaMailSender对象创建MimeMessage
            MimeMessage message = mailSender.createMimeMessage();

            //2. 交予MimiMessageHelper帮助构建
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);//【true】表示支持html格式的文本

            //3. JavaMailSender对象发送构建好的message
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            logger.error("发送邮件失败:" + e.getMessage());//记录错误日志
        }
    }

}

 三、模板引擎

  • 使用Thymeleaf发送HTML邮件
  1. 在templates文件下编写html格式的邮件模板
  2. 编写测试类,发送文本邮件和HTML格式的邮件
    import com.nowcoder.community.community.util.MailClient;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ContextConfiguration;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;
    
    @SpringBootTest
    @ContextConfiguration(classes = CommunityApplication.class)
    public class MailTests {
    
        @Autowired
        private MailClient mailClient;
    
        @Test
        public void testTextMail(){
            mailClient.sendMail("444945173@qq.com","Text","Welcome!");
        }
    
        @Autowired
        private TemplateEngine templateEngine;
    
        @Test
        public void testTtmlMail(){
            Context context = new Context();
            context.setVariable("username","sunday");
    
            //返回具有html格式的字符串
            String content = templateEngine.process("mail/demo", context);
            System.out.println(content);
    
            mailClient.sendMail("444945173@qq.com","HTML",content);
        }
    
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李巴巴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值