找不到类型为'org.springframework.mail.javamail.JavaMailSender'的Bean

h22fl7wq  于 2023-05-06  发布在  Spring
关注(0)|答案(8)|浏览(349)

我正在使用spring boot 2.0.7 Releasespring-boot-starter-mail-2.0.7.Release.
我在类内部自动装配javaMailsender,在windows上工作正常,同时尝试在Unix上部署,遇到以下问题

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
mftmpeh8

mftmpeh81#

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();
    }

}

你也可以从上面的@Bean返回一个JavaMailSenderImpl()的示例,这样当你试图返回实际的JavaMailSender()类时,你就不必再去实现一堆方法了。

eit6fx6z

eit6fx6z2#

你必须在 * application.properties * 中提供邮件配置

spring.mail.host=MAIL_SERVER_IP
spring.mail.port=MAIL_SERVER_PORT
spring.mail.userName=USER_NAME
spring.mail.password=THE_PASSWORD

如果未在服务器中启用身份验证,则删除userName和password并添加此

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

nbysray53#

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

@Configuration
public class MyConfig {

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

确保您也在application.properties下设置了正确的属性。
另外,看看这个question,因为我相信这是一个副本(如果不是,我很抱歉)

pgky5nke

pgky5nke4#

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.
beq87vna

beq87vna5#

我找到的最佳答案是检查www.example.com中是否有类型application.properties:

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

检查Could not autowire org.springframework.mail.javamail.JavaMailSendergleidson cardoso da silva的响应

5ssjco0h

5ssjco0h6#

发生此错误的原因是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
fafcakar

fafcakar7#

我有这个错误,它是有关在application.yml在测试文件夹中丢失的属性。

aij0ehis

aij0ehis8#

This message is a Spring Boot application startup error, and it occurs when the application tries to create an EmailService object but cannot find the JavaMailSender object it needs.

该错误表明程序无法在Sping Boot 应用程序的配置中找到类型为'org.springframework.mail.javamail.javaMailSender'的bean。为了解决这个问题,你必须在Sping Boot 的配置文件中定义应用程序需要的bean,并确保它是可访问的。
可以在Sping Boot 应用程序的配置文件中添加以下代码,以定义类型为'org.springframework.mail.javamail. JavaMailSender'的Bean:

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

@Configuration
public class EmailConfig {

    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        // set mailSender properties (host, port, username, password, etc.)
        return mailSender;
    }
}

必须在已使用的电子邮件帐户上指定适当的JavaMailSender属性。然后,必须在启用调试选项的情况下重新启动应用程序,以查看显示有关问题的更多详细信息的报告。

相关问题