如果spring.mail.host不在application.properties

时间:2016-05-24 13:41:07

标签: java spring spring-boot javamail autowired

我有一点问题"使用JavaMailSenderImpl在我的spring启动应用程序中发送电子邮件。

我正在尝试动态设置所有属性(我希望将来可以从数据库中读取它们)但是,由于我不知道的原因,自动装配JavaMailSenderImpl仅在" spring.mail.host&#34时有效;存在于我的application.properties中。

我设置的值并不重要(它可以是空的,它并不重要,因为我之后设置了正确的值),但属性必须在那里或自动装配将失败。

这是我的控制器:

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:

可以正常工作(发送电子邮件)
#springboot-starter-mail properties
spring.mail.host=

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

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

我可以把空房子留在那里,但感觉不对。

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

2 个答案:

答案 0 :(得分:2)

自动连接JavaMailSender接口而不是实现。

@Autowired
private JavaMailSender mailSender;

答案 1 :(得分:2)

不要自动装配JavaMailSenderImpl。而是创建一个创建JavaMailSenderImpl实例的方法,并使用您需要的所有参数返回它。然后在您需要的地方拨打您的邮件发件人。如果您自动装配mailsender,它会注入一个已经需要参数spring.mail.host

的实例