依赖
<!--邮件模块依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
properties
#email
spring.mail.host=smtp.qq.com
spring.mail.username=邮箱地址@qq.com
spring.mail.password=qnuwuviceychbfef
spring.mail.port=587
spring.mail.from=邮箱地址@qq.com
spring.mail.protocol=smtp
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.stmp.ssl.enable=true
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器连接端口号
spring.redis.port=6379
# 连接池中最大连接数(使用负值则表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池中最大阻塞等待时间(使用负值则表示没有限制)
spring.redis.jedis.pool.max-wait=-1ms
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间
spring.redis.timeout=3000ms
接口
package com.etc.service;
/**
* @Author Dj
* @Description
* @Date 2021/3/21 18:05
**/
public interface MailService {
void sendSimpleMail(String to, String subject, String content);
}
实现类
package com.etc.service.impl;
import com.etc.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
/**
* @Author Dj
* @Description
* @Date 2021/3/21 18:05
**/
@Service
public class MailServiceIlpl implements MailService {
// springboot 提供的邮件 注入即可
@Autowired
private JavaMailSender mailSender;
// 配置邮箱
@Value("${spring.mail.from}")
private String from;
/**
*
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
// 创建 SimpleMailMessage 对象
SimpleMailMessage message = new SimpleMailMessage();
// 邮件发送人
message.setFrom(from);
// 邮件接收人
message.setTo(to);
// 邮件主题
message.setSubject(subject);
// 邮件内容
message.setText(content);
// 发送邮件
mailSender.send(message);
}
}
conteoller
public RusultBody addUser(String username,String password,String mail){
RusultBody rusultBody = new RusultBody();
User user = new User(username,password,mail);
Integer add = userService.addUser(user);
// 生成六位随机码
String content = String.valueOf(new Random().nextInt(899999) + 100000);
// 发送邮件 主题 内容
mailService.sendSimpleMail(mail,"邮箱注册验证码!","你的注册验证码为:"+content);
redisTemplate.opsForValue().set("code:",content, 5,TimeUnit.MINUTES);