title: JavaMailSenderImpl autowire error if spring.mail.host not in application.properties
body: <p>I am having a little "problem" using JavaMailSenderImpl to send emails in my spring boot application.</p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>This is my Controller:</p>
  
  <pre><code>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";
      }
  
  }
  </code></pre>
  
  <p>Will work fine (sends the email) with this application.properties:</p>
  
  <pre><code>#springboot-starter-mail properties
  spring.mail.host=
  </code></pre>
  
  <p>But will throw this exception if I remove that line:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I could leave the empty property there but It doesn't feel right.</p>
  
  <p>Any ideas what might be the cause?</p>