spring-mail-starter 忽略 spring.mail.* 配置
spring-mail-starter ignoring spring.mail.* config
我有一个项目使用 spring-boot 并添加
我的 build.gradle 中的以下依赖项:
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '1.5.7.RELEASE'
我的application.properties包含
SMTP 配置
spring.mail.host=mail.xxx
spring.mail.port=587
spring.mail.username=donotreply@xxx
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.from=donotreply@xxx
根据此处的多个答案和我找到的文档,这应该足以使用此设置创建 bean。
我现在想在服务中使用邮件程序 class
@Autowired
private JavaMailSender emailSender;
[...]
@Value("${spring.mail.host}")
private String mailHost;
当我尝试使用 emailSender 发送消息时,出现以下错误:
Mail server connection failed; nested exception is
com.sun.mail.util.MailConnectException: Couldn't connect to host,
port: localhost, 25; timeout -1; nested exception is:
java.net.ConnectException: Connection refused: connect. Failed
messages: com.sun.mail.util.MailConnectException: Couldn't connect to
host, port: localhost, 25; timeout -1; nested exception is:
java.net.ConnectException: Connection refused: connect
显然它正在尝试连接到本地主机而不是配置的主机。
变量 mailHost 确实包含正确的值,因此属性读取正确。
编辑:
emailSender 的自动装配 class 是 org.springframework.mail.javamail.JavaMailSenderImpl,这对我来说似乎是正确的。
[找到]
一位同事正在做同样的工作,并且提交了一个不完整的 bean 定义。该 bean 是自动装配的。
在我的例子中,原因是我自己实例化了这个 bean:
@Bean
public JavaMailSender mailSender() throws IOException {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setDefaultEncoding("UTF-8");
return mailSender;
}
不知道为什么,但是那样的话,它会忽略 application.properties
。也许原因是,我显式实例化了 JavaMailSenderImpl 以设置默认编码,并且可能 application.properties
不知道该实现?
但是,删除那个 bean 解决了问题。
我有一个项目使用 spring-boot 并添加 我的 build.gradle 中的以下依赖项:
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '1.5.7.RELEASE'
我的application.properties包含
SMTP 配置
spring.mail.host=mail.xxx
spring.mail.port=587
spring.mail.username=donotreply@xxx
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.from=donotreply@xxx
根据此处的多个答案和我找到的文档,这应该足以使用此设置创建 bean。
我现在想在服务中使用邮件程序 class
@Autowired
private JavaMailSender emailSender;
[...]
@Value("${spring.mail.host}")
private String mailHost;
当我尝试使用 emailSender 发送消息时,出现以下错误:
Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1; nested exception is: java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1; nested exception is: java.net.ConnectException: Connection refused: connect
显然它正在尝试连接到本地主机而不是配置的主机。 变量 mailHost 确实包含正确的值,因此属性读取正确。
编辑: emailSender 的自动装配 class 是 org.springframework.mail.javamail.JavaMailSenderImpl,这对我来说似乎是正确的。
[找到] 一位同事正在做同样的工作,并且提交了一个不完整的 bean 定义。该 bean 是自动装配的。
在我的例子中,原因是我自己实例化了这个 bean:
@Bean
public JavaMailSender mailSender() throws IOException {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setDefaultEncoding("UTF-8");
return mailSender;
}
不知道为什么,但是那样的话,它会忽略 application.properties
。也许原因是,我显式实例化了 JavaMailSenderImpl 以设置默认编码,并且可能 application.properties
不知道该实现?
但是,删除那个 bean 解决了问题。