From 8a438e648cd982c501725810d900b45fb385c83c Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Tue, 2 Jan 2024 18:46:22 +0100 Subject: [PATCH] Correctly use start TLS for emails Decouple the transport (SMTP / SMTPS) from whether or not STARTTLS needs to be send --- .../impl/cfg/mail/FlowableMailClientCreator.java | 8 +++----- .../common/impl/BaseMailHostServerConfiguration.java | 10 ++++++++++ .../mail/common/impl/MailHostServerConfiguration.java | 3 ++- .../jakarta/mail/JakartaMailFlowableMailClient.java | 4 ++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/cfg/mail/FlowableMailClientCreator.java b/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/cfg/mail/FlowableMailClientCreator.java index 1ba1420a63b..928b073b50f 100644 --- a/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/cfg/mail/FlowableMailClientCreator.java +++ b/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/cfg/mail/FlowableMailClientCreator.java @@ -62,16 +62,14 @@ protected static MailHostServerConfiguration createMailHostServerConfiguration(S if (mailServer.isMailServerUseSSL()) { serverConfiguration.setPort(mailServer.getMailServerSSLPort()); - if (mailServer.isMailServerUseSSL()) { - serverConfiguration.setTransport(MailHostServerConfiguration.Transport.SMTPS_TLS); - } else { - serverConfiguration.setTransport(MailHostServerConfiguration.Transport.SMTPS); - } + serverConfiguration.setTransport(MailHostServerConfiguration.Transport.SMTPS); } else { serverConfiguration.setPort(mailServer.getMailServerPort()); serverConfiguration.setTransport(MailHostServerConfiguration.Transport.SMTP); } + serverConfiguration.setStartTlsEnabled(mailServer.isMailServerUseTLS()); + serverConfiguration.setUser(mailServer.getMailServerUsername()); serverConfiguration.setPassword(mailServer.getMailServerPassword()); return serverConfiguration; diff --git a/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/BaseMailHostServerConfiguration.java b/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/BaseMailHostServerConfiguration.java index ae110442109..4b46399908d 100644 --- a/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/BaseMailHostServerConfiguration.java +++ b/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/BaseMailHostServerConfiguration.java @@ -21,6 +21,7 @@ public class BaseMailHostServerConfiguration implements MailHostServerConfigurat protected int port = 25; protected Transport transport = Transport.SMTP; + protected boolean startTlsEnabled; protected String user; protected String password; @@ -52,6 +53,15 @@ public void setTransport(Transport transport) { this.transport = transport; } + @Override + public boolean isStartTlsEnabled() { + return startTlsEnabled; + } + + public void setStartTlsEnabled(boolean startTlsEnabled) { + this.startTlsEnabled = startTlsEnabled; + } + @Override public String user() { return user; diff --git a/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/MailHostServerConfiguration.java b/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/MailHostServerConfiguration.java index 0fe04056c5f..cb797d70030 100644 --- a/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/MailHostServerConfiguration.java +++ b/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/MailHostServerConfiguration.java @@ -27,10 +27,11 @@ public non-sealed interface MailHostServerConfiguration extends MailServerConfig String password(); + boolean isStartTlsEnabled(); + enum Transport { SMTP, SMTPS, - SMTPS_TLS, } } diff --git a/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/jakarta/mail/JakartaMailFlowableMailClient.java b/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/jakarta/mail/JakartaMailFlowableMailClient.java index 4df67a2709d..dc19341f3b7 100644 --- a/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/jakarta/mail/JakartaMailFlowableMailClient.java +++ b/modules/flowable-mail/src/main/java/org/flowable/mail/common/impl/jakarta/mail/JakartaMailFlowableMailClient.java @@ -379,7 +379,7 @@ protected Session createSession(MailHostServerConfiguration serverConfiguration) properties.setProperty(MAIL_HOST, host); MailHostServerConfiguration.Transport transport = serverConfiguration.transport(); - properties.setProperty(MAIL_TRANSPORT_STARTTLS_ENABLE, transport == MailHostServerConfiguration.Transport.SMTPS_TLS ? "true" : "false"); + properties.setProperty(MAIL_TRANSPORT_STARTTLS_ENABLE, Boolean.toString(serverConfiguration.isStartTlsEnabled())); properties.setProperty(MAIL_TRANSPORT_STARTTLS_REQUIRED, "false"); properties.setProperty(MAIL_SMTP_SEND_PARTIAL, "false"); @@ -390,7 +390,7 @@ protected Session createSession(MailHostServerConfiguration serverConfiguration) properties.setProperty(MAIL_SMTP_AUTH, "true"); } - if (transport == MailHostServerConfiguration.Transport.SMTPS || transport == MailHostServerConfiguration.Transport.SMTPS_TLS) { + if (transport == MailHostServerConfiguration.Transport.SMTPS) { properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_PORT, String.valueOf(serverConfiguration.port())); properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_CLASS, "javax.net.ssl.SSLSocketFactory"); properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_FALLBACK, "false");