Spring项目发送QQ邮件

出错了,但是百度找不到解决问题的方案,Spring项目中发送QQ邮件的出现以下报错:


```java
ERROR] Errors:  [ERROR]   SpringDemo01ApplicationTests.contextLoads » IllegalState Failed to load ApplicationContext for [WebMergedContextConfiguration@6fad5ace testClass = com.example.springdemo01.SpringDemo01ApplicationTests, locations = [], classes = [
下面是我的配置文件:

```xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringDemo01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringDemo01</name>
    <description>SpringDemo01</description>
    <properties>
        <java.version>18</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <forkCount>0</forkCount>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


下面是我的发送代码:

package com.example.springdemo01;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.File;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
class SpringDemo01ApplicationTests {
    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    public void contextLoads() {
        //发送邮件
        SimpleMailMessage message=new SimpleMailMessage();
        message.setSubject("测试Spring");
        message.setText("Hello,Spring!");
        message.setFrom("2974166992@qq.com");
        message.setTo("1769537228@qq.com");
        mailSender.send(message);
    }
    @Test
    public void test2() throws MessagingException {
        MimeMessage mimeMessage=mailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("测试Spring-2");
        helper.setText("<h1>Hello,Spring!</h1>",true);
        helper.addAttachment("1.jpg",new File("D:\\java workplace\\SpringDemo01\\src\\test\\resources\\1.JPG"));
        helper.setFrom("2974166992@qq.com");
        helper.setTo("1769537228@qq.com");
        mailSender.send(mimeMessage);
    }
}

springboot配置文件配置的啥,用QQ邮箱发邮件需要提供QQ邮箱的信息并在QQ邮箱进行设置的

配置的yml贴出来

你这个是SpringbootTest写的不对,参考这个博客改下试试
https://blog.csdn.net/dayuiicghaid/article/details/124119507