需要找不到类型为'org.springframework.mail.javamail.JavaMailSender'的bean,但是给出了配置

时间:2018-12-13 08:26:29

标签: java email spring-boot javabeans

我想在SpringBoot gradle项目中使用JavaMailSender发送电子邮件,但是我收到错误消息,指出该电子邮件的配置无效。

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

bootstrap.properties中有电子邮件配置。我提供了主机,端口,协议以及用户名和密码。

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

以及EmailServiceImpl.java中的代码。 @autowired无效。

private final TemplateEngine templateEngine;
private final JavaMailSender javaMailSender;

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

build.gradle文件中有依赖项。

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')
}

您能告诉我如何解决此错误吗?谢谢。

1 个答案:

答案 0 :(得分:1)

从外观上看,我认为这是由于服务器配置所致。我正在为Gradle和Maven都提供代码(我知道您正在使用Gradle,但是使用Maven的人可能会遇到相同的问题,所以我同时为它们提供了解决方案)

对于Gradle,将它们添加到application.properties文件中,然后将它们添加

spring.mail.username=youremail@domain.com
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

对于Maven,将其添加到您的server.xml文件中:

<mailSession description="" from="" host="127.0.0.1" jndiName="" mailSessionID="" transportProtocol="smtp" user="youremail@domain.com" password="yourpassword">
                   <property name="mail.smtp.auth" value="true"/>
                   <property name="mail.smtp.port" value="25"/>
               </mailSession>

希望这会有所帮助!