当前位置:   article > 正文

springboot邮箱验证_spring 邮件校验

spring 邮件校验
一、Springboot代码和配置

1.pom.xml

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.application.yml

spring:
  mail:
    protocol: smtp
    host: smtp.qq.com
    username: luckymfc@qq.com
    #配置密码,注意不是真正的密码,而是邮箱申请到的授权码
    password: nqgjgumcebsojagc
    # 端口号465或587
    port: 587
    # 默认的邮件编码为UTF-8
    default-encoding: UTF-8

3.代码

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

@Autowired
private JavaMailSender mailSender;
public void  sendMail(){
    // 生成随机验证码
    String code = RandomUtil.randomNumbers(4);
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo("接收者邮箱");
    message.setSubject("Password Recovery");
    message.setText("Your verification code is: " + code);
    mailSender.send(message);
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/686372
推荐阅读
相关标签
  

闽ICP备14008679号