-
获取邮箱授权码
登录邮箱------->点击设置------->点击账户,下拉------->找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务------->按照步骤开启POP3/SMTP服务 ------->获取授权码
2.在pom.xml中添加依赖:
<!--mail--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>2.6.9</version></dependency>
3.配置yml文件:
mail:host: smtp.qq.com #不同的邮箱不一致,请自行百度查询port: 587 #qq邮箱默认username: 1916403680@qq.com #发送邮箱password: ******** #授权码# 设置是否需要认证,如果为true,那么用户名和密码就必须的,# 如果设置false,可以不设置用户名和密码,当然也得看你的对接的平台是否支持无密码进行访问的。auth=true
4.建实体类等等:
具体操作看这
5.写配置文件:
spring: mail:host: smtp.qq.com #不同的邮箱不一致,请自行百度查询port: 587 #qq邮箱默认username: 1916403680@qq.com #发送邮箱password: zsxhbrjtylbibccc #授权码auth: false #不需要邮件密码
6.写一个工具类用来存放随机验证码方法:
/*** 随机生成验证码工具类*/
public class ValidateCodeUtils {/*** 随机生成验证码* @param length 长度为4位或者6位* @return*/public static Integer generateValidateCode(int length){Integer code =null;if(length == 4){code = new Random().nextInt(9999);//生成随机数,最大为9999if(code < 1000){code = code + 1000;//保证随机数为4位数字}}else if(length == 6){code = new Random().nextInt(999999);//生成随机数,最大为999999if(code < 100000){code = code + 100000;//保证随机数为6位数字}}else{throw new RuntimeException("只能生成4位或6位数字验证码");}return code;}/*** 随机生成指定长度字符串验证码* @param length 长度* @return*/public static String generateValidateCode4String(int length){Random rdm = new Random();String hash1 = Integer.toHexString(rdm.nextInt());String capstr = hash1.substring(0, length);return capstr;}
}
7.写发送邮件的方法:
public interface UserService extends IService<User> {public String send(User user);
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {@Autowiredprivate JavaMailSender mailSender;//获取配置文件的邮件账号@Value("${spring.mail.username}")private String from;@Overridepublic String send(User user) {//生成随机数String code = ValidateCodeUtils.generateValidateCode(4).toString();try {SimpleMailMessage mailMessage = new SimpleMailMessage();//主题mailMessage.setSubject("验证码邮件");//内容mailMessage.setText("您收到的验证码是:" + code);System.out.println("您收到的验证码是:" + code);//发给谁mailMessage.setTo(user.getPhone());//你自己的邮箱(可以去配置文件中获取)mailMessage.setFrom(from);//发送mailSender.send(mailMessage);} catch (Exception e) {e.printStackTrace();return code;}return code;}
}
8.写控制类:
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {@Autowiredprivate UserService userService;/*** 发送邮件短信验证码* @param user* @return*/@PostMapping("/sendMsg")public R<String> sendMsg(@RequestBody User user, HttpSession session){//获取邮箱String phone = user.getPhone();if(StringUtils.isNotEmpty(phone)){//发送验证码到邮箱String code = userService.send(user);//需要将生成的验证码保存到Sessionsession.setAttribute(phone,code);return R.success("手机验证码短信发送成功");}return R.error("短信发送失败");}/*** 移动端登录* @param map* @param session* @return*/@PostMapping("/login")public R<User> sendMsg(@RequestBody Map map, HttpSession session){//获取邮箱String phone = map.get("phone").toString();//获取验证码String code = map.get("code").toString();//从Session中获取保存的验证码Object codeInSession = session.getAttribute(phone);//进行验证码的比对(页面提交的验证码和Session中保存的验证码比对)if(codeInSession != null && codeInSession.equals(code)){//如果能够比对成功,说明登录成功LambdaQueryWrapper<User> eq = Wrappers.lambdaQuery(User.class).eq(User::getPhone, phone);User user = userService.getOne(eq);if(user == null){//判断当前邮箱账号对应的用户是否为新用户,如果是新用户就自动完成注册user = new User();user.setPhone(phone);user.setStatus(1);userService.save(user);}session.setAttribute("user",user.getId());return R.success(user);}return R.error("登录失败");}
}