找不到类型为“ org.springframework.mail.javamail.JavaMailSender”的bean

时间:2019-07-18 11:42:04

标签: spring-boot

我正在使用spring boot 2.0.7 Releasespring-boot-starter-mail-2.0.7.Release.

我正在尝试在javaMailsender上进行部署时出现问题的情况下自动在Unix的类中在Windows上自动接线


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

Description:

Field javaMailSender in com.fti.di.capstock.tran.pub.email.SendEmail required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

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

    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.messaging.Message;
    import org.springframework.messaging.MessagingException;
    import org.springframework.stereotype.Component;

    import com.fti.di.capstock.tran.pub.constants.ApplicationFlowConstants;
    import com.fti.di.integration.constants.IntegrationConstants;
    import com.fti.di.integration.util.StringUtil;

    @Component("sendEmail")
    public class SendEmail {

        @Autowired
        private JavaMailSender javaMailSender;

        @Autowired
        Environment env;

        @ServiceActivator

6 个答案:

答案 0 :(得分:2)

您必须在 application.properties

中提供邮件配置
spring.mail.host=MAIL_SERVER_IP
spring.mail.port=MAIL_SERVER_PORT
spring.mail.userName=USER_NAME
spring.mail.password=THE_PASSWORD

如果服务器中未启用身份验证,则 删除用户名和密码并添加这个

spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.starttls.enable=false

答案 1 :(得分:1)

在Configuration类中声明类型为JavaMailSender的@Bean(当您要注入不属于Spring Context的类时,这很有用,例如属于第三方库的类,这恰好是您的情况)。例如:

@Configuration
public class MyConfig {

    @Bean
    public JavaMailSender javaMailSender() { 
          return new JavaMailSender();
    }
}

请确保您还在application.properties下设置了正确的属性。

另外,请查看此question,因为我认为这是重复的(如果不是,很抱歉)

答案 2 :(得分:0)

In My project i was reading email properties file like hostname, port etc from spring config server (Spring cloud). 
I was missing a dependency at client end. Once i added that Dependency.

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

JavamailSender class able to read those properties and worked fine.
In spring boot we need not define JavaMailSender bean manually. spring boot does itself. 

答案 3 :(得分:0)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
public class Config {

    @Bean
    public JavaMailSender javaMailSender() {
        return new JavaMailSenderImpl();
    }

}

您还可以从上方的JavaMailSenderImpl()返回@Bean的实例,省去了在尝试返回实际的JavaMailSender()类时必须实现一堆方法的麻烦。 / p>

答案 4 :(得分:0)

我发现的最佳答案是检查您的 application.properties 中是否有类型:

spring.mail.host
spring.mail.username
spring.mail.password
spring.mail.port

查看来自 Could not autowire org.springframework.mail.javamail.JavaMailSender

gleidson cardoso da silva 的回复

答案 5 :(得分:0)

发生此错误是由于 application.yml 文件中缺少以下属性。

  mail:
    host: localhost
    port: 1025
    username: hello
    password: hello

    properties:
      mail:
        smtp:
          ssl:
            trust: "*"
          auth: true
          starttls:
            enable: true
          connectiontimeout: 5000
          timeout: 3000
          writetimeout: 5000