qq邮箱部分
-
打开qq邮箱,点击设置—>账户
-
在账户里寻找POP/SMTP服务
-
发送验证码
代码部分
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>;
- 撰写Java代码(使用了redis存储验证码,自己选择)
下面展示一些内联代码片
。
public boolean sendMimeMail( String email, HttpSession session) {
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("验证码邮件");
//生成随机数
String code = getrandom();
//将随机数放置到session中
session.setAttribute("email",email);
session.setAttribute("code",code);
ValueOperations operations = redisTemplate.opsForValue ();
operations.set( "code",code );
operations.set ( "code",code,15, TimeUnit.MINUTES );//15是过期时间,后面是过期时间的单位
mailMessage.setText("您收到的验证码是:"+code);//内容
mailMessage.setTo(email);//设置目标邮箱
mailMessage.setFrom(from);//自己的邮箱
mailSender.send(mailMessage);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
/**
* 随机生成6位数的验证码
* @return String code
*/
public String getrandom(){
StringBuilder str = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 6; i++) {
str.append(random.nextInt(10));
}
return str.toString();
};