SpringBoot发送QQ邮箱

SpringBoot发送QQ邮箱

前言:

因项目有部分功能要发送企业内部邮箱,要用到QQ邮箱测试下网段是否通,用于排查下问题。

发送邮箱

1,导入依赖

c 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2,QQ邮箱开通SMTP协议

发送邮件简单说,需要有个中介转发下 ,这个中介是SMTP协议

发送者 -->> SMTP协议 -->> 接收者

3,编写发送邮件代码案例

yml配置

c 复制代码
# yml的配置文件
  mail:
    host: smtp.qq.com
    # 发送者信息,注意密码是授权码,不是密码
    username: 1xxx4149@qq.com
    password: erwxxbpfxxcajg
    # 接收着邮箱
    receiver: 26xx822xx4@qq.com
    default-encoding: utf-8
    protocol: smtp
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

controller:

c 复制代码
package com.xxx.xxx.platform.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author psd
 */
@Slf4j
@Api(tags = "QQ邮箱")
@RestController
@RequestMapping("platform/qqEmail")
public class QqEmailController {

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


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

    @Autowired
    private JavaMailSender javaMailSender;


    @GetMapping("/sentQqEmail")
    public void sentQqEmail(){
        log.info("配置文件中的信息 userName:{},receiver:{}",userName,receiver);
        SimpleMailMessage message = new SimpleMailMessage();
        // 发送着
        message.setFrom(userName);
//        message.setTo(receiver);
        message.setSubject("测试发送邮箱的主题");
        message.setText("我是文本信息 测试是否发送成功");
        javaMailSender.send(message);
    }

}

注意事项:

1,配置密码不是,邮箱的密码是授权码,【理解可能不对:就是在SMTP上面做一个假登录】

2,QQ邮箱一定要开通,支持SMTP协议

喜欢我的文章记得点个在看,或者点赞,持续更新中ing...

相关推荐
野生的码农8 小时前
码农的妇产科实习记录
android·java·人工智能
盖世英雄酱581369 小时前
Java 组长年终总结:靠 AI 提效 50%,25 年搞副业只赚 4k?
后端·程序员·trae
毕设源码-赖学姐9 小时前
【开题答辩全过程】以 高校人才培养方案管理系统的设计与实现为例,包含答辩的问题和答案
java
+VX:Fegn089510 小时前
计算机毕业设计|基于springboot + vue在线音乐播放系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
一起努力啊~10 小时前
算法刷题-二分查找
java·数据结构·算法
小途软件10 小时前
高校宿舍访客预约管理平台开发
java·人工智能·pytorch·python·深度学习·语言模型
J_liaty10 小时前
Java版本演进:从JDK 8到JDK 21的特性革命与对比分析
java·开发语言·jdk
code bean10 小时前
Flask图片服务在不同网络接口下的路径解析问题及解决方案
后端·python·flask
+VX:Fegn089510 小时前
计算机毕业设计|基于springboot + vue律师咨询系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
努力的小郑10 小时前
2025年度总结:当我在 Cursor 里敲下 Tab 的那一刻,我知道时代变了
前端·后端·ai编程