Aeseir Aeseir - 2 months ago 19
Java Question

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

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.

Answer

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.

Comments