在日常生活中,我们在一个网站中注册一个账户时,往往在提交个人信息后,网站还要我们通过手机或邮件来验证,邮件的话大概会是下面这个样子的:

1. 使用SpringBoot整合邮件发送服务 - 图1

1.1 邮件发送过程

1. 使用SpringBoot整合邮件发送服务 - 图2

1.2 邮件发送一般会设计到几个协议:SMTP和POP3

image.png

1.3 开通邮件发送的SMTP和POP3协议的服务

image.png
image.png

2.1 创建SpringBoot工程

2.1.1 配置POM依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.3</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.gmw.boot</groupId>
  12. <artifactId>send_meail</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>send_meail</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-mail</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-devtools</artifactId>
  35. <scope>runtime</scope>
  36. <optional>true</optional>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.projectlombok</groupId>
  40. <artifactId>lombok</artifactId>
  41. <optional>true</optional>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-test</artifactId>
  46. <scope>test</scope>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. <configuration>
  55. <excludes>
  56. <exclude>
  57. <groupId>org.projectlombok</groupId>
  58. <artifactId>lombok</artifactId>
  59. </exclude>
  60. </excludes>
  61. </configuration>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>

加入这个核心依赖

image.png

2.1.2 配置application.application文件

#端口号
server.port=8888

#配置thymeleaf模板引擎
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false

#配置邮件设置
#配置QQ邮箱的host的地址
spring.mail.host=smtp.qq.com
#开通QQ邮箱的用户名
spring.mail.username=ganmaowang@qq.com
#设置密码(但要注意,不是设置QQ密码,而是指定发送)
spring.mail.password=vunqpeajrjzmbiac
#设置属性,指定邮件的信息进行加密
spring.mail.properties.mail.smtp.ssl.enable=true

2.1.3 编写发送邮箱的代码

package com.gmw.boot.send_meail;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@SpringBootTest
class SendMeailApplicationTests {
    //要发送邮件,需要注入javaMailSender
    @Autowired
    private JavaMailSenderImpl javaMailSender;
    @Value("${spring.mail.username}")
    public String from;

    /**
     * 简单邮件发送测试:(不携带附件的邮件,就称为:简单邮件)
     */
    @Test
    void contextLoads() {
        //创建一个简单的邮箱
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //发送给谁
        simpleMailMessage.setTo("751729326@qq.com");
        //发送的主题
        simpleMailMessage.setSubject("未进化的程序猿给你来一条劲爆的消息");
        //发送的消息内容
        simpleMailMessage.setText("前面有一群母猪排队掉到水坑里,哈哈!!!");
        //发送人
        simpleMailMessage.setFrom(from);

        //发送
        javaMailSender.send(simpleMailMessage);

        System.out.println("邮件发送成功!");
    }

}

2.1.4 进行代码的测试

image.png