SpringBoot发送QQ邮箱

SpringBoot发送QQ邮箱

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

发送邮箱

1,导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

2,QQ邮箱开通SMTP协议
发送邮件简单说,需要有个中介转发下 ,这个中介是SMTP协议
发送者 -->> SMTP协议 -->> 接收者
在这里插入图片描述

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

# 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:

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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中配置QQ邮箱,你需要进行以下准备工作: 1. 导入相关依赖: 在你的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>2.5.6</version> </dependency> ``` 2. 编写Spring Boot配置文件: 在application.properties配置文件中添加以下配置: ```properties # 配置邮箱用户名 spring.mail.username=你的QQ邮箱号码 # 配置申请到的授权码 spring.mail.password=刚才短信申请到的授权码 # 配置SMTP服务器地址 spring.mail.host=smtp.qq.com # 配置邮件编码 spring.mail.default-encoding=UTF-8 ``` 3. 在Spring Boot测试类中发送邮件: 在Spring Boot的测试类中,通过JavaMailSenderImpl对象的send方法发送邮件,示例代码如下: ```java @SpringBootTest class SpringbootLoginDemo1ApplicationTests { @Autowired JavaMailSenderImpl mailSender; //自动注入对象 @Test void contextLoads() { //创建一个简单的邮件信息 SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setSubject("你好啊"); //邮件的主题 mailMessage.setText("我是你新学习的Spring Boot邮件发送的知识"); //邮件的内容 mailMessage.setTo("发送者的邮箱"); //接收者的邮箱 mailMessage.setFrom("接收者的邮箱"); //发送者的邮箱 mailSender.send(mailMessage); //发送邮件 } } ``` 以上就是配置QQ邮箱的步骤。你可以根据自己的需求进行相应的修改和扩展。如果你遇到了其他问题,可以继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值