springboot 发送邮箱验证

本文介绍了如何在Spring Boot应用中通过依赖`spring-boot-starter-mail`集成邮件服务,并配置了SMTP服务器和Redis连接。重点展示了MailService接口和其实现类,以及在用户注册流程中发送验证码的代码片段。
摘要由CSDN通过智能技术生成

依赖

        <!--邮件模块依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

properties

#email
spring.mail.host=smtp.qq.com
spring.mail.username=邮箱地址@qq.com
spring.mail.password=qnuwuviceychbfef
spring.mail.port=587

spring.mail.from=邮箱地址@qq.com
spring.mail.protocol=smtp
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.stmp.ssl.enable=true

# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器连接端口号
spring.redis.port=6379
# 连接池中最大连接数(使用负值则表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池中最大阻塞等待时间(使用负值则表示没有限制)
spring.redis.jedis.pool.max-wait=-1ms
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间
spring.redis.timeout=3000ms

接口

package com.etc.service;

/**
 * @Author Dj
 * @Description
 * @Date 2021/3/21 18:05
 **/
public interface MailService {

    void sendSimpleMail(String to, String subject, String content);


}

实现类

package com.etc.service.impl;

import com.etc.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @Author Dj
 * @Description
 * @Date 2021/3/21 18:05
 **/
@Service
public class MailServiceIlpl implements MailService {


    // springboot 提供的邮件 注入即可
    @Autowired
    private JavaMailSender mailSender;

    // 配置邮箱
    @Value("${spring.mail.from}")
    private String from;

    /**
     *
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     */
    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        // 创建 SimpleMailMessage 对象
        SimpleMailMessage message  = new SimpleMailMessage();
        // 邮件发送人
        message.setFrom(from);
        // 邮件接收人
        message.setTo(to);
        // 邮件主题
        message.setSubject(subject);
        // 邮件内容
        message.setText(content);
        // 发送邮件
        mailSender.send(message);

    }
}

conteoller

    public RusultBody addUser(String username,String password,String mail){
        RusultBody rusultBody = new RusultBody();
        User user = new User(username,password,mail);
        Integer add = userService.addUser(user);
        // 生成六位随机码
        String content = String.valueOf(new Random().nextInt(899999) + 100000);
        // 发送邮件 主题 内容
        mailService.sendSimpleMail(mail,"邮箱注册验证码!","你的注册验证码为:"+content);
        redisTemplate.opsForValue().set("code:",content, 5,TimeUnit.MINUTES);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值