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.