小工具知识网

当前位置: 首页 > 行业知识 > Java完成邮件发送(QQ邮箱为例)

行业知识

Java完成邮件发送(QQ邮箱为例)

2026-10-25 20:28:00 来源:互联网转载

Java完成邮件发送,保姆级教学(QQ邮箱为例)

1.开启QQ邮箱第三方服务(我的已经开启了)

继续按照要求操作、获取到授权码

2.导入依赖

                            org.springframework.boot            spring-boot-starter-mail                                     org.springframework.boot            spring-boot-starter-data-redis        

3.编写配置文件

spring:  # mail邮箱  mail:    # SMTP服务器(我用的是QQ邮箱的SMTP服务器地址,如果用的其它邮箱请另行百度搜索)    host: smtp.qq.com    port: 465    # 发送验证码的邮箱(发件人的邮箱)    username: ***********@qq.com    # 授权码    password: **************    # 编码    default-encoding: utf-8    # 其它参数    properties:      mail:        smtp:          # 如果是用SSL方式,需要配置如下属性,使用qq邮箱的话需要开启          ssl:            enable: true            required: true          # 邮件接收时间的限制,单位毫秒          timeout: 10000          # 连接时间的限制,单位毫秒          connection-timeout: 10000          # 邮件发送时间的限制,单位毫秒          write-timeout: 10000  redis:    host: 127.0.0.1 # 主机    port: 6379 # 端口号    jedis:      pool:        max-active: 8   #最大活跃的连接数,最多几个连接        max-idle: 8   #最多空闲连接数        max-wait: -1ms  #最大等待时间  -1一直等待        min-idle: 0   #最小空闲数

4.编写生成验证码及校验验证码的工具类

/** * 验证码相关工具类 */@Component@Slf4jpublic class VerifyCodeUtil {     @Resource    private StringRedisTemplate stringRedisTemplate;     /**     * 随机生成指定长度字符串验证码     *     * @param length 验证码长度     */    public String generateVerifyCode(int length) {        String strRange = "1234567890";        StringBuilder strBuilder = new StringBuilder();         for (int i = 0; i  

5.编写发送邮箱验证码接口

①controller层

/** * @Author: one piece * @Description: 邮箱验证码 * @Date: 2023/11/12 11:31 */@RestController@RequestMapping("/mail")@Slf4jpublic class MailController {    @Resource    private MailService mailService;    @PostMapping("/sendMail")    public String sendMail(@RequestBody String email , HttpServletRequest request) throws UnsupportedEncodingException {        log.debug(email);        //由于编码问题会把@符号解析为@40所以进行如下处理        String encodedEmail = request.getParameter("email");        String decodedEmail = URLDecoder.decode(encodedEmail, "UTF-8");        log.info(decodedEmail);        String message = mailService.sendEmail(decodedEmail);        return message;    }

②Service层

/** * @Author: one piece * @Description: * @Date: 2024/2/13 14:21 */@Service@Slf4jpublic class MailService {    @Resource    private VerifyCodeUtil verifyCodeUtil;    @Resource    private JavaMailSender javaMailSender;    @Resource    private StringRedisTemplate stringRedisTemplate;    public String sendEmail(String email) {        if (email.isEmpty()){            return "邮箱不能为空";        }        // 定义Redis的key        String key = "msg_" + email;        ValueOperations valueOperations = stringRedisTemplate.opsForValue();        String verifyCode = valueOperations.get(key);        if (verifyCode == null) {            // 随机生成一个6位数字型的字符串            String code = verifyCodeUtil.generateVerifyCode(6);            // 邮件对象(邮件模板,根据自身业务修改)            SimpleMailMessage message = new SimpleMailMessage();            message.setSubject("验证码");            message.setText("尊敬的用户您好!\n\n感谢您的关注。\n\n尊敬的: " + email + "您的校验验证码为: " + code + ",有效期5分钟,请不要把验证码信息泄露给其他人,如非本人请勿操作");            message.setTo(email);            try {                // 对方看到的发送人(发件人的邮箱,根据实际业务进行修改,一般填写的是企业邮箱,*号位置写发件者邮箱)                message.setFrom(new InternetAddress(MimeUtility.encodeText("one piece") + "").toString());                // 发送邮件                javaMailSender.send(message);                // 将生成的验证码存入Redis数据库中,并设置过期时间                valueOperations.set(key, code, 5, TimeUnit.MINUTES);                log.debug(message.toString());                return "验证码已发送至您的邮箱,请注意查收";            } catch (Exception e) {                return "发送失败";            }        } else {            return "验证码已发送至您的邮箱,请注意查收";        }    }

如果对大家有帮助请点个赞、点个关注!!!

上一篇:服务器硬盘怎么存放的?

下一篇:魅族签名证书详解,轻松掌握证书签名信息的实用指南!