题主贴出的配置和代码有两个问题:
1、端口号。根据QQ邮箱的官方说明,端口号可以是465或587,但我测试的时候465是超时的,587就正常。
2、运行贴出的代码尝试发邮件,会报错,报错信息
java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
根据这个信息,host和port这两个参数都没有作为参数传入,运行的时候压根就没有连去QQ邮箱的服务器。
因为弄了一轮都发不出,于是归零重新找资料实现。
下面是我的代码,参考的是spring的官方文档。
配置文件沿用题主的。用了springboot,版本号2.2.3.RELEASE。
package zsh.sf_answer1.mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
<