一、前期准备

一个邮箱,并且开通了SMTP服务
参考:邮箱如何开启POP3/SMTP服务

二、相关依赖

项目依托于springboot,部分依赖不作声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
<!-- web项目依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
<scope>provided</scope>
</dependency>
<!-- 邮箱相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

三、配置文件

此处只给出相关的部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#邮箱配置
#平台地址,这里用的是126邮箱,使用其他邮箱请更换
spring.mail.host = smtp.126.com
#改成自己的邮箱
spring.mail.username = 你的邮箱地址
#发送短信开通服务后它给你的授权码 填写到这里
spring.mail.password =授权码
#这东西不用改
spring.mail.properties.mail.smtp.ssl.enable=true
#编码格式
spring.mail.default-encoding=UTF-8

#redis 服务配置
spring.redis.host=主机
spring.redis.port=端口号
spring.redis.database=你的redis仓库号(一般就0)
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0

四、Service层

接口省略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Created by IntelliJ IDEA.
* User: Zengc
* Date: 2021/7/17
* Time: 10:41
**/
@Service
public class MsmServiceImpl implements MsmService {

@Autowired
private JavaMailSender mailSender;

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

@Override
public String sendMineMail(String email) {
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 设置邮件主题
mailMessage.setSubject("测试邮箱验证码");
// 工具类生成随机六位验证码
String code = RandomCodeUtils.get6BitRandom();
// 设置邮件内容
mailMessage.setText("您收到的验证码是:"+code);
// 设置收发人
mailMessage.setFrom(from);
mailMessage.setTo(email);
// 发送
mailSender.send(mailMessage);
return code;
} catch (MailException e) {
e.printStackTrace();
return "";
}
}
}

五、Controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Created by IntelliJ IDEA.
* User: Zengc
* Date: 2021/7/17
* Time: 10:37
**/
@Api(description = "发送邮件")
@RestController
@RequestMapping("/api/msm")
public class MsmApiController {

@Autowired
private MsmService msmService;

@Autowired
private RedisTemplate<String, String> redisTemplate;

@ApiOperation("发送邮件")
@GetMapping("/send/{email}")
public R code(
@ApiParam(name = "email", value = "邮箱", required = true)
@PathVariable String email
){
String code = redisTemplate.opsForValue().get(email);
if (!StringUtils.isEmpty(code)){
return R.ok().message("邮件已经发送过了,请稍后再试!");
}
// 发送邮件,返回验证码
String codeResult = msmService.sendMineMail(email);
if (!StringUtils.isEmpty(codeResult)){
// 添加缓存并设置五分钟有效
redisTemplate.opsForValue().set(email,codeResult,5, TimeUnit.MINUTES);
return R.ok();
}
return R.error();
}
}

七、测试

启动项目,浏览器访问路径(注意加上邮箱参数)即可测试
在这里插入图片描述

补上工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Created by IntelliJ IDEA.
* User: Zengc
* Date: 2021/7/17
* Time: 11:17
**/
public class RandomCodeUtils {
/**
* 获取四位验证码
* @return
*/
public static String get4BitRandom(){
return String.valueOf((new Random().nextDouble())).substring(2,6);
}

/**
* 获取六位验证码
* @return
*/
public static String get6BitRandom(){
return String.valueOf((new Random().nextDouble())).substring(2,8);
}

}