SpringBoot邮箱验证码

1. 前端

     <el-form-item prop="code">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 55%"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <el-button @click="sendEmailCode" :disabled="codeBtn.isCodeBtnUse" :type="codeBtn.codeType" plain>{{ codeBtn.codeBtnText }}</el-button>
        </div>
      </el-form-item>
export default {
  name: "Register",
  data() {
    return {
      codeBtn:{
        codeBtnText: "发送验证码",
        codeType: 'primary',
        isCodeBtnUse: false,
      },
    };
  },
  methods: {
     sendEmailCode() {
      const username = this.registerForm.username
      var codeTime = 15;
      this.codeBtn.codeType = ''
      this.codeBtn.isCodeBtnUse = true
      var timer = setInterval(() => {
        if (codeTime == -1) {
          this.codeBtn.codeType = 'primary'
          this.codeBtn.isCodeBtnUse = false
          this.codeBtn.codeBtnText = "发送验证码"
          clearInterval(timer)
        } else{
          this.codeBtn.codeBtnText = codeTime + 's后重新发送'
          codeTime--
        }
      }, 1000)
      sendEmailCode(username).then(res => {
        console.log(res)
        if (res.code == 0) {
          this.$message({
            message: res.msg,
            type: 'success'
          })
        }
      }).catch(err => {
        console.log(err)
      })

    }
  },

2. pom依赖

        <!-- 邮箱验证码-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.3.7.RELEASE</version>
        </dependency>

        <!-- HuTool工具-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.17</version>
        </dependency>

3. yml配置

spring:
  mail:
    host: smtp.qq.com
    port: 587
    username: xxxxxxxxx@qq.com
    password: xxxxxxxxx   #password为邮箱授权码
    protocol: smtp #发送邮件协议
    default-encoding: utf-8
    from: xxxxxxxxx@qq.com

4. Controller

    @PostMapping("/sendEmailCode")
    public AjaxResult sendEmailCode(@RequestBody String username)
    {
        String verifCode = RandomUtil.randomNumbers(6);
        String message = "【U独】本次注册验证码:" + verifCode;
        mailService.sendMail(username, "【U独】注册验证码", message);
        return new AjaxResult(0, "验证码发送成功");
    }

5. Service

Service接口

public interface MailService {
    public void sendMail(String sendTo, String title, String content);
}

ServiceImpl实现类

public class MailServiceImpl implements MailService{

    @Resource
    private JavaMailSender mailSender;

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

    public void sendMail(String sendTo, String title, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);  // 发件人
        message.setTo(sendTo);       // 收件人
        message.setSubject(title);   // 邮件标题
        message.setText(content);    // 邮件内容
        mailSender.send(message);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
怎么实现? 可以通过利用Spring Boot中的Java MailSender和Random类来实现发送邮箱验证码的功能。首先,需要在Spring Boot的配置文件中配置SMTP服务器的相关信息,然后在代码中实现生成随机验证码并发送邮件的逻辑。可以参考下面的代码实现: @Configuration public class MailConfiguration { // 邮件发送方 @Value("${spring.mail.username}") private String from; // SMTP服务器地址 @Value("${spring.mail.host}") private String host; // SMTP服务器端口号 @Value("${spring.mail.port}") private int port; // 邮箱账户名 @Value("${spring.mail.username}") private String username; // 邮箱账户密码 @Value("${spring.mail.password}") private String password; // 是否开启SSL @Value("${spring.mail.properties.mail.smtp.ssl.enable}") private boolean sslEnabled; // 是否开启TLS @Value("${spring.mail.properties.mail.smtp.starttls.enable}") private boolean tlsEnabled; @Bean public JavaMailSender getJavaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost(host); mailSender.setPort(port); mailSender.setUsername(username); mailSender.setPassword(password); Properties props = mailSender.getJavaMailProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", tlsEnabled); props.put("mail.smtp.ssl.enable", sslEnabled); return mailSender; } } @Service public class MailService { @Autowired private JavaMailSender javaMailSender; // 生成验证码 public String generateVerificationCode() { Random random = new Random(); int code = random.nextInt(899999) + 100000; return String.valueOf(code); } // 发送邮件 public void sendMail(String to, String subject, String content) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content); javaMailSender.send(message); } } 在上面的代码中,先在配置文件中设置了SMTP服务器的相关信息。然后,在MailService类中,实现了生成随机验证码和发送邮件的方法。generateVerificationCode方法使用Java的Random类来生成六位数的验证码,sendMail方法则使用JavaMailSender发送邮件。发送邮件时,需要创建一个MimeMessage实例,并设置相关的属性(邮件发送方、接收方、主题和内容等)。最后,调用JavaMailSender的send方法发送邮件即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值