loveloper.dev
loveloper.dev

Reputation: 521

JavaMailSender error - NoSuchBeanDefinitionException: No qualifying bean of type

I am developing Java web with JDK 1.6 .

Project that I develop is copied from previous out project which is developed with JDK 1.8 . So I modified many maven dependencies's version. And now, most of them work well. But there is a problem that makes tomcat can't run web.

Whenever I start tomcat(version is 6.0.53), I face an error like this ↓

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

I did search about 'No qualifying bean of type' error via google. People who faced 'No qualifying bean of type' error said "you should check whether you missed annotation service, repository or other".

But I can't add any annotation on JavaMailSender because it is in jar. ↓ These are dependencies I added into pom.xml .

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
</dependency>

How can I solve this problem?

I need your help.

Thank you.

Upvotes: 1

Views: 575

Answers (1)

jeno
jeno

Reputation: 72

You need to create a bean in your config package(if u have it). You are probably having spring security in your dependencies. Your can create a bean as given below:

@Configuration
public class MailConfiguration {

    @Bean
    public JavaMailSender getJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(587);

        mailSender.setUsername("[email protected]");
        mailSender.setPassword("mypassword");

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        return mailSender;
    }
}

Upvotes: 1

Related Questions