I have a little "problem" using JavaMailSenderImpl to send letters in my spring boot application.
I try to set all properties dynamically (I want them to be read from the database in the future), but for unknown reasons, the JavaMailSenderImpl auto-messaging only works if "spring.mail.host" is present in my .properties applications.
It doesnβt matter what value I set (it may be empty, it doesnβt matter because I set the correct one later), but the property should be there, or auto-connection 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 an email) using this application.properties:
But throw this exception if I remove this 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 that doesn't seem right.
What ideas might be the reason?
source share