E.W
E.W

Reputation: 11

required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found, but configs were given

I want to send email using JavaMailSender in a SpringBoot gradle project, but I got error that the configure of the email didn't work.

17:44:47.004 [main ,] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
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: {}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:818)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:724)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:197)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1267)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1124)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:818)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:724)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:197)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1267)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1124)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
	at com.quduoduo.userbackend.UserbackendApplication.main(UserbackendApplication.java:18)
17:44:47.005 [main ,] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in com.quduoduo.userbackend.service.impl.EmailServiceImpl required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:52323', transport: 'socket'

Process finished with exit code 1

There are the email configuration in bootstrap.properties. I gave the host, port, protocol and username&password.

spring.mail.default-encoding=utf-8
spring.mail.smtp.auth=true
spring.mail.smtp.starttls.enable=true
spring.mail.smtp.host=smtp.gmail.com
spring.mail.smtp.port=587
spring.mail.username=xxxx
spring.mail.password=xxxx
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000

And the code in EmailServiceImpl.java. The @autowired didn't work.

private final TemplateEngine templateEngine;
private final JavaMailSender javaMailSender;

@Autowired
public EmailServiceImpl(TemplateEngine templateEngine,
                        JavaMailSender javaMailSender) {
    this.templateEngine = templateEngine;
    this.javaMailSender = javaMailSender;
}

There are the dependencies in the build.gradle file.

dependencies {
    compile group:"cn.com.antube", name:"antube-commons", version:"0.0.3-SNAPSHOT", changing:true
    compile(group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-redis')
    compile('org.springframework.cloud:spring-cloud-config-client')
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth'
    compile "org.springframework.cloud:spring-cloud-starter-zipkin"
    compile('io.projectreactor:reactor-core')
    compile('org.redisson:redisson:3.7.5')
    compile('io.vavr:vavr-jackson:0.9.2')
    compile('com.alibaba:fastjson:1.2.49')
    compile('org.apache.shiro:shiro-core:1.4.0')
    compile('mysql:mysql-connector-java')
    compile('org.codehaus.groovy:groovy-all:2.4.15')
    compile('com.aliyun:aliyun-java-sdk-dysmsapi:1.0.0')
    compile('com.aliyun:aliyun-java-sdk-core:4.0.8')
    compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2")
    compile('com.auth0:java-jwt:3.0.2')
    compile('org.apache.commons:commons-lang3:3.6')
    compile('org.springframework.boot:spring-boot-starter-activemq')
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.1.1.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '2.1.1.RELEASE'
    testCompile('org.springframework.boot:spring-boot-starter-test:2.0.4.RELEASE')
    testCompile('org.spockframework:spock-core:1.2-groovy-2.4')
    testCompile('org.spockframework:spock-spring:1.1-groovy-2.4')
}

Could you tell me how to solve this bug? Thank you.

Upvotes: 1

Views: 9950

Answers (1)

Vince D
Vince D

Reputation: 137

From what it looks I think it's because of server configuration. I'm giving code for both Gradle and Maven(I know you are using Gradle but the same problem can be faced by someone who is using Maven so I'm providing solutions for both)

For Gradle, add these to your application.properties file and these

[email protected]
spring.mail.password=yourpassword
spring.mail.host=smtp.gmail.com
mail.smtp.port= 25
spring.mail.properties.mail.smtp.outh=true
spring.mail.properties.mail.smtp.starttls.enable=true

And for Maven, add this to your server.xml file:

<mailSession description="" from="" host="127.0.0.1" jndiName="" mailSessionID="" transportProtocol="smtp" user="[email protected]" password="yourpassword">
                   <property name="mail.smtp.auth" value="true"/>
                   <property name="mail.smtp.port" value="25"/>
               </mailSession>

Hope this helps!!

Upvotes: 1

Related Questions