Could not autowire org.springframework.mail.javama

2020-06-07 05:17发布

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 confused as to what is happening here.

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.testmail.app.service.implement.CustomMManagerService.setMailSender(org.springframework.mail.javamail.JavaMailSender); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

My bean generation is located in following file:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.testmail.app")
public class WebConfig extends WebMvcConfigurerAdapter {
 //CODE CODE CODE

    @Bean
    public JavaMailSenderImpl mailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setProtocol("SMTP");
        javaMailSender.setHost("127.0.0.1");
        javaMailSender.setPort(25);

        return javaMailSender;
    }

// CODE CODE CODE
}

Code for CustomMManager:

public interface CustomMManager extends Serializable {
    void sendMail(String mailFrom,String mailTo,String subject,String mailBody);
}

Finally the code for CustomMManagerService:

@Service("mailManager")
public class CustomMManagerService implements CustomMManager {
    private JavaMailSender mailSender;

    @Autowired
    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    @Override
    public void sendMail(final String mailFrom, final String mailTo, final String subject, final String mailBody) {
        try {
            mailSender.send(new MimeMessagePreparator() {
                public void prepare(MimeMessage mimeMessage)
                        throws Exception {
                    MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
                            false, "UTF-8");
                    message.setFrom(mailFrom);
                    message.addTo(mailTo);
                    message.setSubject(subject);
                    message.setText(mailBody, true);
                }
            });
        } catch (MailSendException e) {
            // your codes;
        }
    }
}

Help is really appreciated.

6条回答
Juvenile、少年°
2楼-- · 2020-06-07 05:44

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
查看更多
戒情不戒烟
3楼-- · 2020-06-07 05:49

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

@Bean
public JavaMailSender mailSender() {
查看更多
等我变得足够好
4楼-- · 2020-06-07 05:53
  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

查看更多
走好不送
5楼-- · 2020-06-07 05:56

You may have forgotten to set the following properties:

spring.mail.host
spring.mail.username
spring.mail.password
spring.mail.port
查看更多
手持菜刀,她持情操
6楼-- · 2020-06-07 05:56

As already stated you need to set at least spring.mail.host in your application properties. Spring Boot autoconfiguration only creates a JavaMailSender bean if the property is set: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mail/MailSenderPropertiesConfiguration.java#L38

查看更多
叼着烟拽天下
7楼-- · 2020-06-07 05:57

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.

查看更多
登录 后发表回答