如果spring.mail.host不在application.properties中,则JavaMailSenderImpl自动接线错误 [英] JavaMailSenderImpl autowire error if spring.mail.host not in application.properties

查看:414
本文介绍了如果spring.mail.host不在application.properties中,则JavaMailSenderImpl自动接线错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JavaMailSenderImpl在Spring Boot应用程序中发送电子邮件时,我遇到了一些问题".

I am having a little "problem" using JavaMailSenderImpl to send emails in my spring boot application.

我正在尝试动态设置所有属性(我希望以后再从数据库中读取它们),但是由于我不知道的原因,自动装配JavaMailSenderImpl仅在存在"spring.mail.host"的情况下才有效application.properties.

I am trying to set all properties dynamically (I will want them to be read from the DB in the future) but, for reasons unknown to me, autowiring JavaMailSenderImpl only works if "spring.mail.host" is present in my application.properties.

我设置的值无关紧要(它可以为空,也没关系,因为稍后再设置正确的值),但是该属性必须存在,否则自动装配将失败.

It doesn't matter the value I set (it can be empty, it doesn't matter because I set the right one later), but the property must be there or autowiring will fail.

这是我的控制器:

import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class MailController {

    @Autowired
    private JavaMailSenderImpl ms;


    @RequestMapping("/mail")
    public String send(Model model){

        SimpleMailMessage message;
        String fromEmail="sdfsdf98435sadf@gmail.com";
        String toEmail ="xxxxxxx";

        Properties mailProperties = new Properties();
        mailProperties.put("mail.smtp.starttls.enable", true);
        mailProperties.put("mail.smtp.ssl.trust", "smtp.gmail.com");

        ms.setHost("smtp.gmail.com");
        ms.setPort(587);
        ms.setUsername("xxxx");
        ms.setPassword("yyyyy");
        ms.setJavaMailProperties(mailProperties);

        message = new SimpleMailMessage();
        message.setSubject("Test email");
        message.setFrom(fromEmail);
        message.setTo(toEmail);
        message.setText("Something something");

        try{
            ms.send(message);
        }
        catch(MailException ex){
            System.err.println(ex.getMessage());
        }
        return "OK";
    }

}

使用此application.properties可以正常工作(发送电子邮件)

Will work fine (sends the email) with this application.properties:

#springboot-starter-mail properties
spring.mail.host=

但是如果我删除该行,则会抛出此异常:

But will throw this exception if I remove that line:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    ... 19 common frames omitted

我可以将空属性留在那里,但是感觉不对.

I could leave the empty property there but It doesn't feel right.

任何想法可能是什么原因?

Any ideas what might be the cause?

推荐答案

请勿自动接线JavaMailSenderImpl.而是创建一个方法,该方法创建JavaMailSenderImpl的实例,并返回所需的所有参数.然后在需要的地方致电您的邮件发件人.如果自动连接邮件发送者,它将注入一个已经需要参数spring.mail.host的实例.

Do not autowire the JavaMailSenderImpl. Instead create a method that creates and instance of JavaMailSenderImpl and returns it with all parameters you need. And then call your mail sender where you need it. If you autowire the mailsender it injects an instance which already needs the parameter spring.mail.host.

这篇关于如果spring.mail.host不在application.properties中,则JavaMailSenderImpl自动接线错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆