Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 944
Aeseir Avatar asked Apr 23 '14 14:04

Aeseir


People also ask

What is JavaMailSender?

public interface JavaMailSender extends MailSender. Extended MailSender interface for JavaMail, supporting MIME messages both as direct arguments and through preparation callbacks. Typically used in conjunction with the MimeMessageHelper class for convenient creation of JavaMail MimeMessages , including attachments etc ...

What is spring boot starter mail?

< artifactId >spring-boot-starter-mail</ artifactId > </ dependency > This dependency is a starter for using JavaMail and can be considered as Spring Framework's email sending support. Step 2: Setting up Application. properties file with configurations required for using Gmail SMTP server.


3 Answers

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.

like image 23
Aeseir Avatar answered Oct 29 '22 19:10

Aeseir


You may have forgotten to set the following properties:

spring.mail.host
spring.mail.username
spring.mail.password
spring.mail.port
like image 158
Gleidson Cardoso da Silva Avatar answered Oct 29 '22 17:10

Gleidson Cardoso da Silva


  1. check application.properties config, such as:

    spring.mail.host=smtp.xxx.com
    [email protected]
    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

like image 15
leimbag Avatar answered Oct 29 '22 18:10

leimbag