SpringBoot整合spring-boot-starter-mail 发送 QQ邮件

SpringBoot整合spring-boot-starter-mail 发送 QQ邮件

1、设置QQ邮箱,查看流程:QQ邮箱——>设置——>账户——>开启POP3/SMTP服务,生成授权码:邮件开启xxxxxxxxx
在这里插入图片描述
在这里插入图片描述
2、导入mail依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-study</artifactId>
        <groupId>org.bc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>e-mail</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
        <dependency>
            <groupId>org.bc</groupId>
            <artifactId>microboot-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>

3、配置yaml相关信息

server:
  port: 80
spring:
  mail:
    host: smtp.qq.com # 一定要使用合法的SMTP
    username: 111111@qq.com # 用户名
    password: xxxxxxx # 是临时生成的密码
    properties:
      mail.smtp.auth: true # 启用SMTP认证
      mail.smtp.starttls.enabled: true # 启用SMTP认证
      mail.smtp.starttls.required: true # 必须采用加密链接

4、编写发送邮件模块

package com.yootk.test;

import com.yootk.StartSpringBootApplication;
import com.yootk.service.IMessageService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;

@ExtendWith(SpringExtension.class) // 使用JUnit5测试工具
@WebAppConfiguration // 启动WEB运行环境
@SpringBootTest(classes = StartSpringBootApplication.class) // 配置程序启动类
public class TestSendEmail { // 编写测试类
    @Autowired
    private JavaMailSender javaMailSender; // 发送邮件工具类
    @Test
    public void testSend() {    // 进行响应测试
        SimpleMailMessage message = new SimpleMailMessage(); // 建立一个简单的邮件结构
        message.setFrom("1111@qq.com");
        message.setTo("181211111@qq.com"); // 邮件的接收者
        message.setSubject("来老师给你的祝福。");
        message.setText("wwwww");
        this.javaMailSender.send(message); // 邮件发送
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农-004

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值