javamailsender注入失败,如果spring.mail.host不在application.properties中,则JavaMailSenderImpl自动连线错误...

I am having a little "problem" using JavaMailSenderImpl to send emails in my spring boot application.

I am trying to set all properties dynamically (I will want them to be read from the DB in the future) but, for reasons unknown to me, autowiring JavaMailSenderImpl only works if "spring.mail.host" is present in my application.properties.

It doesn't matter the value I set (it can be empty, it doesn't matter because I set the right one later), but the property must be there or autowiring will fail.

This is my Controller:

import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.mail.MailException;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class MailController {

@Autowired

private JavaMailSenderImpl ms;

@RequestMapping("/mail")

public String send(Model model){

SimpleMailMessage message;

String fromEmail="sdfsdf98435sadf@gmail.com";

String toEmail ="xxxxxxx";

Properties mailProperties = new Properties();

mailProperties.put("mail.smtp.starttls.enable", true);

mailProperties.put("mail.smtp.ssl.trust", "smtp.gmail.com");

ms.setHost("smtp.gmail.com");

ms.setPort(587);

ms.setUsername("xxxx");

ms.setPassword("yyyyy");

ms.setJavaMailProperties(mailProperties);

message = new SimpleMailMessage();

message.setSubject("Test email");

message.setFrom(fromEmail);

message.setTo(toEmail);

message.setText("Something something");

try{

ms.send(message);

}

catch(MailException ex){

System.err.println(ex.getMessage());

}

return "OK";

}

}

Will work fine (sends the email) with this application.properties:

#springboot-starter-mail properties

spring.mail.host=

But will throw this exception if I remove that line:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]

... 19 common frames omitted

I could leave the empty property there but It doesn't feel right.

Any ideas what might be the cause?

解决方案

Do not autowire the JavaMailSenderImpl. Instead create a method that creates and instance of JavaMailSenderImpl and returns it with all parameters you need. And then call your mail sender where you need it. If you autowire the mailsender it injects an instance which already needs the parameter spring.mail.host.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值