Could not autowire org.springframework.mail.javamail.JavaMailSender

前端 未结 8 744
时光取名叫无心
时光取名叫无心 2021-02-11 22:18

I got the following problem when trying to run my application. Have debugged everything and still nothing.

The IDE is finding the bean without any issue so I\'m very con

相关标签:
8条回答
  • 2021-02-11 22:55

    In my case, I removed something and my project not shown as maven project. (I am using IntelliJ for my Spring Boot application)

    Set project as maven project (Close and reopen IntelliJ. It will show popup) fixes the issue.

    0 讨论(0)
  • 2021-02-11 22:58

    You may have forgotten to set the following properties:

    spring.mail.host
    spring.mail.username
    spring.mail.password
    spring.mail.port
    
    0 讨论(0)
  • 2021-02-11 23:06

    As per comment from mserioli the answer is that the bean must be declared in the configuration file being called at root.

    In this case: Move

    @Bean
        public JavaMailSenderImpl mailSender() 
    

    to

    public class ExtraConfig {
    @Bean
        public JavaMailSenderImpl mailSender() {
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    
            javaMailSender.setProtocol("SMTP");
            javaMailSender.setHost("127.0.0.1");
            javaMailSender.setPort(25);
    
            return javaMailSender;
        }
    }
    

    which is called in:

    @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[]{ExtraConfig.class};
        }
    

    Thus solving the problem. Thanks guys for assistance.

    0 讨论(0)
  • 2021-02-11 23:08

    to create Bean don't forgot to specify mail properties, with Java class or in application.properties file, exemple

    # configuration email
    spring.mail.host=smtp.gmail.com
    spring.mail.port=587
    spring.mail.username=email
    spring.mail.password=password
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.starttls.required=true
    
    0 讨论(0)
  • 2021-02-11 23:09
    1. check application.properties config, such as:

      spring.mail.host=smtp.xxx.com
      spring.mail.username=xxx@xxx.com
      spring.mail.password=xxxxx
      spring.mail.properties.mail.smtp.auth=true
      spring.mail.properties.mail.smtp.starttls.enable=true
      spring.mail.properties.mail.smtp.starttls.required=true
      
    2. if you use spring-boot,can check should use @EnableAutoConfiguration this annotation

    0 讨论(0)
  • 2021-02-11 23:11

    Have you tried to declare your bean returning the interface implemented? Something like this:

    @Bean
    public JavaMailSender mailSender() {
    
    0 讨论(0)
提交回复
热议问题