邮件发送验证码

邮件发送接口

由于阿里云短信发送服务没法用,所以把短信接口改成邮件接口
图片说明

1.配置邮箱使用的yml

#qq邮箱注册
  mail:
    host: smtp.qq.com
    username: 915262772@qq.com
    password: ikyxdzozsjstbaii

2.controller层

@Api(tags = "前台邮箱发送接口")
@RestController
@RequestMapping("/api/sms")
@CrossOrigin
public class smsController   {

    @Resource
    private SmsService smsService;

    @ApiOperation("邮件发送方法")
    @GetMapping("/send/{mailAddress}")
    public Result sendMessage(@PathVariable String mailAddress) {
//        获取4位验证码
        String templateCode = RandomUtils.getFourBitRandom();
//         将验证码放入redis中
        RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.opsForValue().set("srb:sms:code:"+mailAddress,templateCode);
//        调用service发送验证码
        smsService.send(mailAddress, templateCode);
        System.out.println("邮件发送方法调用成功");
        return Result.ok().message("邮件发送成功");
    }}

2.service层

所调用的service层 也就是smsService

public class SmsServiceImpl implements SmsService  {

    @Resource
    private JavaMailSender mailSender;
    private final static String myForm = "915262772@qq.com";


    @Override
    public void send(String mailAddress, String templateCode) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//        邮件地址
        simpleMailMessage.setFrom(myForm);
//        邮件发送地址
        simpleMailMessage.setTo(mailAddress);
        String codeText = "您的验证码是" + templateCode + ",请妥善保存,60s后失效。";
        simpleMailMessage.setText(codeText);
        try {
            mailSender.send(simpleMailMessage);
        } catch (MailException e) {
            System.out.println(e.getMessage());
        }}}
全部评论

相关推荐

object3:开始给部分🌸孝子上人生第一课了
点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务