无法自动装配 org.springframework.mail.javamail.JavaMailSender

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23246912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:30:17  来源:igfitidea点击:

Could not autowire org.springframework.mail.javamail.JavaMailSender

javaspringspring-mvc

提问by Aeseir

I got the following problem when trying to run my application. Have debugged everything and still nothing.

尝试运行我的应用程序时遇到以下问题。已经调试了一切,仍然没有。

The IDE is finding the bean without any issue so I'm very confused as to what is happening here.

IDE 正在毫无问题地找到 bean,所以我对这里发生的事情感到非常困惑。

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.testmail.app.service.implement.CustomMManagerService.setMailSender(org.springframework.mail.javamail.JavaMailSender); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

My bean generation is located in following file:

我的 bean 生成位于以下文件中:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.testmail.app")
public class WebConfig extends WebMvcConfigurerAdapter {
 //CODE CODE CODE

    @Bean
    public JavaMailSenderImpl mailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setProtocol("SMTP");
        javaMailSender.setHost("127.0.0.1");
        javaMailSender.setPort(25);

        return javaMailSender;
    }

// CODE CODE CODE
}

Code for CustomMManager:

CustomMManager 的代码:

public interface CustomMManager extends Serializable {
    void sendMail(String mailFrom,String mailTo,String subject,String mailBody);
}

Finally the code for CustomMManagerService:

最后是 CustomMManagerService 的代码:

@Service("mailManager")
public class CustomMManagerService implements CustomMManager {
    private JavaMailSender mailSender;

    @Autowired
    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    @Override
    public void sendMail(final String mailFrom, final String mailTo, final String subject, final String mailBody) {
        try {
            mailSender.send(new MimeMessagePreparator() {
                public void prepare(MimeMessage mimeMessage)
                        throws Exception {
                    MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
                            false, "UTF-8");
                    message.setFrom(mailFrom);
                    message.addTo(mailTo);
                    message.setSubject(subject);
                    message.setText(mailBody, true);
                }
            });
        } catch (MailSendException e) {
            // your codes;
        }
    }
}

Help is really appreciated.

非常感谢帮助。

采纳答案by Aeseir

As per comment from mserioli the answer is that the bean must be declared in the configuration file being called at root.

根据 mserioli 的评论,答案是必须在根目录调用的配置文件中声明 bean。

In this case: Move

在这种情况下:移动

@Bean
    public JavaMailSenderImpl mailSender() 

to

public class ExtraConfig {
@Bean
    public JavaMailSenderImpl mailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        javaMailSender.setProtocol("SMTP");
        javaMailSender.setHost("127.0.0.1");
        javaMailSender.setPort(25);

        return javaMailSender;
    }
}

which is called in:

这被称为:

@Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{ExtraConfig.class};
    }

Thus solving the problem. Thanks guys for assistance.

从而解决问题。谢谢大家的帮助。

回答by Gix

Have you tried to declare your bean returning the interface implemented? Something like this:

您是否尝试声明您的 bean 返回已实现的接口?像这样的东西:

@Bean
public JavaMailSender mailSender() {

回答by Gleidson Cardoso da Silva

You may have forgotten to set the following properties:

您可能忘记设置以下属性:

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

回答by leimbag

  1. check application.properties config, such as:

    spring.mail.host=smtp.xxx.com
    [email protected]
    spring.mail.password=xxxxx
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.starttls.required=true
    
  2. if you use spring-boot,can check should use @EnableAutoConfiguration this annotation

  1. 检查 application.properties 配置,例如:

    spring.mail.host=smtp.xxx.com
    [email protected]
    spring.mail.password=xxxxx
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.starttls.required=true
    
  2. 如果你使用 spring-boot,可以检查是否应该使用 @EnableAutoConfiguration 这个注解

回答by Walterwhites

to create Bean don't forgot to specify mail properties, with Java class or in application.properties file, exemple

创建 Bean 时不要忘记指定邮件属性,使用 Java 类或在 application.properties 文件中,例如

# configuration email
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=email
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

回答by dtrunk

As already stated you need to set at least spring.mail.hostin your application properties. Spring Boot autoconfiguration only creates a JavaMailSenderbean if the property is set: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mail/MailSenderPropertiesConfiguration.java#L38

如前所述spring.mail.host,您至少需要在应用程序属性中进行设置。JavaMailSender如果设置了属性,Spring Boot 自动配置只会创建一个bean:https: //github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java /org/springframework/boot/autoconfigure/mail/MailSenderPropertiesConfiguration.java#L38