即使自动连线,SpringBoot JavaMailSender也为null

时间:2019-05-14 21:14:36

标签: java spring spring-boot http smtp

我正在尝试向我的Web应用程序实现邮件发送功能。问题出在JavaMailSender上,无论是在运行应用程序还是进行单元测试时,它都不会自动连接。我真的不明白为什么它不想实例化JavaMailSender对象。我已经正确设置了application.properties和application-test.properties。

application.properties和application-test.properties

spring.mail.host=mail.xxx.com
spring.mail.username=noreply@xxx.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.port=587
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

我的EmailSender.class

@Component
public class EmailSender {

    private static final Logger log = LoggerFactory.getLogger(EmailSender.class);

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private Configuration freeMarkerConfig = null;
    @Autowired
    private JujuServerProperties serverProperties = null;

    private List<RuntimeTypeAdapterFactory> adapterFactoryList;

    private AbstractSamplePostSynchroActions abstractSamplePostSynchroActions;

    //FIXME CE CODE EST A 2 ENDROITS (SendEmailProcessor)
    public void send(int projectId,int questionnaireId, int terminalId) {
        try {

            // TODO: Revoir certaind crite pour l<envoi de courrier test. Ceci devrait inclure la presence des entree postSynchro dans la BD. valider la correspondance avec une question email dans le questionnaire
            adapterFactoryList = new ArrayList<>();
            adapterFactoryList.add(prepareMediaQuestionAdapters());
            adapterFactoryList.add(prepareSocialMediaAdapters());
            adapterFactoryList.add(prepareConditionAdapters());

            ObjectMapper mapper = new ObjectMapper();
            File fic = new File(IOUtils.getTerminalDir(projectId, questionnaireId,terminalId)+ File.separator+"Email"+File.separator+"email.json");

            Email emailConfig = EmailUtil.generateEmailConfigurationFromJsonFile(fic,adapterFactoryList);

            ConditionsEvaluator conditionsEvaluator = emailConfig.getConditionsEvaluator();

            Map<String, Object> model = new HashMap<>();
            PostSynchroUtil.generateEmail(projectId,questionnaireId,terminalId,adapterFactoryList);
            File file = new File(IOUtils.getTerminalDir(projectId, questionnaireId,terminalId)+"Email"+File.separator+"email.xml");
            //initEmailModel(model, respondent);
            SampleEmailData sample = (SampleEmailData)abstractSamplePostSynchroActions;
            MimeMessagePreparator preparator = mimeMessage -> {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                for(QuestionCondition questionCondition : conditionsEvaluator.getConditions())
                {
                    if(questionCondition.getQuestion_type().contains("email"))
                    {

                        String emailAddr = sample.getEmailAddrs();
                        message.setTo(emailAddr);
                    }
                }

                message.setFrom("noreply@nomadlogicapp.com","noreply@nomadlogicapp.com");
                message.setSubject("Test Email From Juju Console");
                StringWriter out = new StringWriter();




                message.setText(sample.getMsg(),true);
            };

            this.mailSender.send(preparator);


        } catch (Exception ex) {
            log.error("{}", ex);
        }
    }

    private void initEmailModel(Map<String, Object> model, IRespondent respondent, Email email) {
        model.put("email", email);
        model.put("respondent", respondent);
        model.put("server", serverProperties.getServerAdress());

    }

    public AbstractSamplePostSynchroActions getAbstractSamplePostSynchroActions() {
        return abstractSamplePostSynchroActions;
    }

    public void setAbstractSamplePostSynchroActions(AbstractSamplePostSynchroActions abstractSamplePostSynchroActions) {
        this.abstractSamplePostSynchroActions = abstractSamplePostSynchroActions;
    }
}

Spring Configuration类

package com.nomadlogic.juju;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


@ConfigurationProperties(prefix = "juju-server")
@Component
public class JujuServerProperties {
    private String address;
    private String port;
    private String dns;

    public String getServerAdress() {
        return this.address + ":" + this.port;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDns() {
        return dns;
    }

    public void setDns(String dns) {
        this.dns = dns;
    }
}

0 个答案:

没有答案