无法从spring中的application.yml加载有效属性(jhipster)

cnh2zyt3  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(343)

我正在与jhipster(spring)合作创建一个具有很多功能的微服务,其中之一就是发送电子邮件。
jhipster应用程序没有用于电子邮件发送的样板代码,而jhipster网关有。所以我从gateway应用程序复制了代码,并根据需要进行了调整。
这是我复制的代码,基本上它有一个由jhipster制作的发送电子邮件的函数。

@Service
public class MailService {

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

    private final JHipsterProperties jHipsterProperties;

    private final JavaMailSender javaMailSender;

    public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender) {

        this.jHipsterProperties = jHipsterProperties;
        this.javaMailSender = javaMailSender;
    }

    @PostConstruct
    void sendmail() {
        // here where I send an email for testing
        sendEmail("myemailhere@example.com", "subject here", "content here", false, false);
        log.debug("EMAIL SENT SUCCESFULLY");
    }

    @Async
    public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
        log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
            isMultipart, isHtml, to, subject, content);

        // Prepare message using a Spring helper
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, StandardCharsets.UTF_8.name());
            message.setTo(to);
            message.setFrom(jHipsterProperties.getMail().getFrom());
            message.setSubject(subject);
            message.setText(content, isHtml);
            javaMailSender.send(mimeMessage);
            log.debug("Sent email to User '{}'", to);
        }  catch (MailException | MessagingException e) {
            log.warn("Email could not be sent to user '{}'", to, e);
        }
    }
}

这是我的application-dev.yml文件:

mail:
    host: my.host.here
    port: 465
    username: myemailhere@example.com
    password: mypasswordhere
    protocol: smtp
    tls: true
    properties.mail.smtp:
      auth: true
      starttls.enable: true

当我运行代码时,它从属性文件中加载无效信息,并引发以下错误,我不确定它的行为是这样的?

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2210)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:722)
    at javax.mail.Service.connect(Service.java:342)
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518)
    at org.springframework.mail.javamail.JavaMailSenderImpl.testConnection(JavaMailSenderImpl.java:398)
    at org.springframework.boot.actuate.mail.MailHealthIndicator.doHealthCheck(MailHealthIndicator.java:42)
    at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:82)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1675)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
    at org.springframework.cloud.netflix.eureka.EurekaHealthCheckHandler.getStatus(EurekaHealthCheckHandler.java:195)
    at org.springframework.cloud.netflix.eureka.EurekaHealthCheckHandler.getHealthStatus(EurekaHealthCheckHandler.java:176)
    at org.springframework.cloud.netflix.eureka.EurekaHealthCheckHandler.getStatus(EurekaHealthCheckHandler.java:170)
    at com.netflix.discovery.DiscoveryClient.refreshInstanceInfo(DiscoveryClient.java:1419)
    at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:117)
    at com.netflix.discovery.InstanceInfoReplicator$1.run(InstanceInfoReplicator.java:101)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:609)
    at java.base/java.net.Socket.connect(Socket.java:558)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:335)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:214)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2160)
    ... 25 common frames omitted

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题