【带小白做项目】如何在SpringBoot项目中实现邮件发送功能?

目录

一 开启客户端SMTP服务

        1.开启邮箱SMTP服务

        2.账号验证

二  Spring Email相关配置

        3.jar包导入

        4.添加依赖

        5.在application.properties中进行相关配置

        6.编写邮件发送工具类代码

        7.编写测试用例

        8.发送HTML邮件

        9.测试


一 开启客户端SMTP服务

        首先我们需要在发件邮箱中,开启邮箱的SMTP服务,这里我们以QQ邮箱为例。

1.开启邮箱SMTP服务

登录QQ邮箱,找到“设置”----“账号设置”----“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”,开启服务。

2.账号验证

初次开启服务需要使用手机账号进行验证,验证成功后,会出现如下提示,这个授权码需要记好,后面还需要用到。

二  Spring Email相关配置

3.jar包导入

下面我们需要在项目中导入相关依赖。我们登录maven仓库(Maven Repository),找到Spring mail相关的。选择第一个就可以。

选择一个合适的版本(这里选择的是2.2.6.RELEASE 版本,需要注意如果选择2.2.x以后的版本在后续的操作上有所差异)

4.添加依赖

复制这里的代码到pom.xml文件中,添加相关依赖,等待依赖下载完成

        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
			<version>2.2.6.RELEASE</version>
		</dependency>
5.在application.properties中进行相关配置

需要注意的是这里的spring.mail.host根据你使用的邮箱不同要进行对应的修改,例如新浪邮箱就是smtp.sina.com。spring.mail.properties.mail.smtp.port也要根据邮箱的不同进行改动。

# MailProperties
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
spring.mail.username=邮箱地址
spring.mail.password=授权码
spring.mail.protocol=smtps
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.port=587
6.编写邮件发送工具类代码

@Component
public class MailClient {
    private static final Logger logger = LoggerFactory.getLogger(MailClient.class);

    @Autowired(required = false)
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private  String from;
    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);
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            logger.error("发送邮件失败:" + e.getMessage());
        }
    }
}
7.编写测试用例
     @Test
    public void testTextMail(){
         mailClient.sendMail("你希望收到邮件的地址","邮件主题","邮件内容");
     }

运行测试用例,如果收件邮箱收到了我们发送的邮件主题和内容,说明配置成功!

测试类完整代码如下:


import com.nowcoder.community.util.MailClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)

public class MailTests {
     @Autowired
     private MailClient mailClient;

     @Autowired
     private TemplateEngine templateEngine;
     @Test
     public void testTextMail(){
         mailClient.sendMail("1092010788@qq.com","Test","12345");
     }
}
8.发送HTML邮件

我们首先需要新建一个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: blue;" th:text="${username}"></span>! </p>
    <p>欢迎使用xx平台注册功能</p>
</body>
</html>
9.测试
    @Test
     public  void testHtmlMail(){
         Context context = new Context();
         context.setVariable("username","July");
         String content= templateEngine.process("/mail/demo",context);
         System.out.println(content);
         mailClient.sendMail("收件邮箱地址","邮件主题",content);
     }

测试结果:

  • 33
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值