springboot实现邮箱接收验证码

记录:使用邮箱接收验证码登录代替手机号接收验证码登录

原因:手机号接收验证码要钱

开通邮箱SMTP服务的步骤:
打开邮箱->设置->账户->找到下图位置->发送短信->保存授权码
在这里插入图片描述

@Component
public class SendMailUtils {
    @Resource
    JavaMailSenderImpl javaMailSender;
    public  void sendEmailCode(String targetEmail, String authCode) {
        SimpleMailMessage mailMessage =new SimpleMailMessage();
        //邮件主题
        mailMessage.setSubject("你好");
        //邮件内容
        mailMessage.setText(authCode);
        //发件人邮箱
        mailMessage.setFrom("发件人邮箱账号");
        //默认为QQ号码,重命名发件人:自定义命名<发件人邮箱账号>
        //mailMessage.setFrom("外卖<发件人邮箱账号>");
        //收件人邮箱
        mailMessage.setTo(targetEmail);
        //发送
        javaMailSender.send(mailMessage);
    }
}

使用工具类时,使用注解注入,不然容易造成JavaMailSenderImpl 空值

@Resource
    SendMailUtils sendMailUtils;

pom.xml文件的设置

#邮箱设置
#平台地址,qq邮箱的服务器地址:smtp.qq.com
spring.mail.host=smtp.qq.com
#25端口以及被禁止,使用会报错
spring.mail.port=465
#发件人邮箱账号
spring.mail.username=*****@qq.com
#授权码,开通邮箱SMTP服务会有一个授权码
spring.mail.password=*****

#使用安全链接
spring.mail.properties.mail.smtp.ssl.enable=true
#编码格式
spring.mail.default-encoding=UTF-8
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 中发送邮箱验证码可以通过使用 JavaMailSender 来实现。首先,你需要在 `application.properties` 或 `application.yml` 配置文件中配置邮件服务器的相关信息,如下所示: application.properties: ```properties spring.mail.host=your.mail.server spring.mail.port=your.mail.port spring.mail.username=your.username spring.mail.password=your.password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` application.yml: ```yaml spring: mail: host: your.mail.server port: your.mail.port username: your.username password: your.password properties: mail: smtp: auth: true starttls: enable: true ``` 接下来,你可以创建一个邮件服务类来发送验证邮件,示例如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender javaMailSender; public void sendVerificationCode(String to, String code) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject("验证码"); message.setText("您的验证码是:" + code); javaMailSender.send(message); } } ``` 在上述示例中,`JavaMailSender` 是由 Spring Boot 自动配置的邮件发送器。你可以在需要发送验证码的地方调用 `sendVerificationCode` 方法来发送邮件。 注意:为了使用JavaMailSender,你需要在项目的依赖管理文件(例如 pom.xml)中添加相应的依赖。你可以添加以下依赖来使用 Spring Boot 提供的邮件支持: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 以上就是使用 Spring Boot 发送邮箱验证码的简单示例。你可以根据自己的实际需求进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只代码小白啦啦啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值