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...

相关推荐
高频因子挖掘机7 分钟前
本地已有一年历史数据,第二天更新别只拉“昨天一天”:量化 K 线增量同步实践
后端·github·api
文丑颜不良啊14 分钟前
LeetCode201~225题目解析
java·leetcode
高频因子挖掘机21 分钟前
只补缺失日期,不重拉全部历史:增量更新 K 线应该怎么设计?
后端·github·api
Gopher_HBo1 小时前
Go reflect 包动态调用与方法反射 —Call、MakeFunc 与性能
后端
码事漫谈2 小时前
人设带处境,AI 更入戏
后端
掘金酱2 小时前
【10.24 特别活动】AI 时代,程序员的新饭碗在哪里?
前端·人工智能·后端
SL_staff2 小时前
4G设备掉线三层归因指南:SIM卡、模组、JVS-IOT网关配置精准锁定
java·物联网·产品
leeyi2 小时前
缓存同步之后,还要考虑没收到消息的节点
后端·性能优化·erlang
IT_陈寒2 小时前
Redis的原子性竟然失效了?记一次分布式锁翻车实录
前端·人工智能·后端
shengjk12 小时前
既然 gRPC 底层也是 HTTP,为什么大厂还非要死磕 RPC?真相全在这!
后端