This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new b98eb60  JAMES-3681 : RemoteDelivery mailet must use correct Java Mail 
property prefix for SMTPS sessions (#783)
b98eb60 is described below

commit b98eb6068c7d96e5235858279039dfd45b90376d
Author: Karsten Otto <[email protected]>
AuthorDate: Thu Dec 9 08:14:34 2021 +0100

    JAMES-3681 : RemoteDelivery mailet must use correct Java Mail property 
prefix for SMTPS sessions (#783)
---
 .../remote/delivery/MailDelivrerToHost.java        | 20 +++++---
 .../delivery/RemoteDeliveryConfiguration.java      | 30 +++++++-----
 .../delivery/RemoteDeliveryConfigurationTest.java  | 56 ++++++++++++++++++++--
 3 files changed, 84 insertions(+), 22 deletions(-)

diff --git 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/MailDelivrerToHost.java
 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/MailDelivrerToHost.java
index 9b3ca01..64686f1 100644
--- 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/MailDelivrerToHost.java
+++ 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/MailDelivrerToHost.java
@@ -67,8 +67,8 @@ public class MailDelivrerToHost {
         this.configuration = remoteDeliveryConfiguration;
         this.converter7Bit = new Converter7Bit(mailetContext);
         if (configuration.isSSLEnable()) {
-            this.smtpSession = 
Session.getInstance(configuration.createFinalJavaxPropertiesNoSSL());
-            this.smtpsSession = 
Session.getInstance(configuration.createFinalJavaxProperties());
+            this.smtpSession = 
Session.getInstance(configuration.createFinalJavaxProperties());
+            this.smtpsSession = 
Session.getInstance(configuration.createFinalJavaxPropertiesWithSSL());
         } else {
             this.smtpSession = 
Session.getInstance(configuration.createFinalJavaxProperties());
             this.smtpsSession = this.smtpSession;
@@ -79,7 +79,7 @@ public class MailDelivrerToHost {
         Session session = selectSession(outgoingMailServer);
         Properties props = getPropertiesForMail(mail, session);
         LOGGER.debug("Attempting delivery of {} to host {} at {} from {}",
-            mail.getName(), outgoingMailServer.getHostName(), 
outgoingMailServer.getHost(), props.get("mail.smtp.from"));
+            mail.getName(), outgoingMailServer.getHostName(), 
outgoingMailServer.getHost(), props.get(inContext(session, "mail.smtp.from")));
 
         // Many of these properties are only in later JavaMail versions
         // "mail.smtp.ehlo"           //default true
@@ -90,7 +90,7 @@ public class MailDelivrerToHost {
         SMTPTransport transport = null;
         try {
             transport = (SMTPTransport) 
session.getTransport(outgoingMailServer);
-            transport.setLocalHost(props.getProperty("mail.smtp.localhost", 
configuration.getHeloNameProvider().getHeloName()));
+            transport.setLocalHost(props.getProperty(inContext(session, 
"mail.smtp.localhost"), configuration.getHeloNameProvider().getHeloName()));
             connect(outgoingMailServer, transport);
             if (mail.dsnParameters().isPresent()) {
                 sendDSNAwareEmail(mail, transport, addr);
@@ -98,7 +98,7 @@ public class MailDelivrerToHost {
                 transport.sendMessage(adaptToTransport(mail.getMessage(), 
transport), addr.toArray(InternetAddress[]::new));
             }
             LOGGER.debug("Mail ({})  sent successfully to {} at {} from {} for 
{}", mail.getName(), outgoingMailServer.getHostName(),
-                outgoingMailServer.getHost(), props.get("mail.smtp.from"), 
mail.getRecipients());
+                outgoingMailServer.getHost(), props.get(inContext(session, 
"mail.smtp.from")), mail.getRecipients());
         } finally {
             closeTransport(mail, outgoingMailServer, transport);
         }
@@ -113,6 +113,14 @@ public class MailDelivrerToHost {
         }
     }
 
+    private String inContext(Session session, String name) {
+        if (session.getProperties().containsKey("mail.smtps.ssl.enable")) {
+            return name.replace("smtp", "smtps");
+        } else {
+            return name;
+        }
+    }
+
     private void sendDSNAwareEmail(Mail mail, SMTPTransport transport, 
Collection<InternetAddress> addresses) {
         addresses.stream()
             .map(address -> Pair.of(
@@ -182,7 +190,7 @@ public class MailDelivrerToHost {
 
     private Properties getPropertiesForMail(Mail mail, Session session) {
         Properties props = session.getProperties();
-        props.put("mail.smtp.from", mail.getMaybeSender().asString());
+        props.put(inContext(session, "mail.smtp.from"), 
mail.getMaybeSender().asString());
         return props;
     }
 
diff --git 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
index 6fdbf97..fa5908c 100644
--- 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
+++ 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
@@ -211,31 +211,37 @@ public class RemoteDeliveryConfiguration {
     }
 
     public Properties createFinalJavaxProperties() {
-        Properties props = createFinalJavaxPropertiesNoSSL();
-        props.put("mail.smtp.ssl.enable", String.valueOf(isSSLEnable));
+        Properties props = createFinalJavaxProperties("smtp");
+        props.put("mail.smtp.ssl.enable", "false");
         return props;
     }
 
-    public Properties createFinalJavaxPropertiesNoSSL() {
+    public Properties createFinalJavaxPropertiesWithSSL() {
+        Properties props = createFinalJavaxProperties("smtps");
+        props.put("mail.smtps.ssl.enable", "true");
+        return props;
+    }
+    
+    private Properties createFinalJavaxProperties(String protocol) {
         Properties props = new Properties();
         props.put("mail.debug", "false");
         // Reactivated: javamail 1.3.2 should no more have problems with "250 
OK" messages
         // (WAS "false": Prevents problems encountered with 250 OK Messages)
-        props.put("mail.smtp.ehlo", "true");
-        props.put("mail.smtp.timeout", String.valueOf(smtpTimeout));
-        props.put("mail.smtp.connectiontimeout", 
String.valueOf(connectionTimeout));
-        props.put("mail.smtp.sendpartial", String.valueOf(sendPartial));
-        props.put("mail.smtp.localhost", heloNameProvider.getHeloName());
-        props.put("mail.smtp.starttls.enable", String.valueOf(startTLS));
+        props.put("mail." + protocol + ".ehlo", "true");
+        props.put("mail." + protocol + ".timeout", 
String.valueOf(smtpTimeout));
+        props.put("mail." + protocol + ".connectiontimeout", 
String.valueOf(connectionTimeout));
+        props.put("mail." + protocol + ".sendpartial", 
String.valueOf(sendPartial));
+        props.put("mail." + protocol + ".localhost", 
heloNameProvider.getHeloName());
+        props.put("mail." + protocol + ".starttls.enable", 
String.valueOf(startTLS));
         if (isBindUsed()) {
             // undocumented JavaMail 1.2 feature, smtp transport will use
             // our socket factory, which will also set the local address
-            props.put("mail.smtp.socketFactory.class", 
RemoteDeliverySocketFactory.class);
+            props.put("mail." + protocol + ".socketFactory.class", 
RemoteDeliverySocketFactory.class);
             // Don't fallback to the standard socket factory on error, do 
throw an exception
-            props.put("mail.smtp.socketFactory.fallback", "false");
+            props.put("mail." + protocol + ".socketFactory.fallback", "false");
         }
         if (authUser != null) {
-            props.put("mail.smtp.auth", "true");
+            props.put("mail." + protocol + ".auth", "true");
         }
         props.putAll(javaxAdditionalProperties);
         return props;
diff --git 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfigurationTest.java
 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfigurationTest.java
index 1fbbba2..65d9de0 100644
--- 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfigurationTest.java
+++ 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfigurationTest.java
@@ -772,7 +772,7 @@ public class RemoteDeliveryConfigurationTest {
         String helo = "domain.com";
         int connectionTimeout = 1856;
         FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
-            .setProperty(RemoteDeliveryConfiguration.SSL_ENABLE, "true")
+            .setProperty(RemoteDeliveryConfiguration.SSL_ENABLE, "false")
             .setProperty(RemoteDeliveryConfiguration.SENDPARTIAL, "true")
             .setProperty(RemoteDeliveryConfiguration.CONNECTIONTIMEOUT, 
String.valueOf(connectionTimeout))
             .setProperty(RemoteDeliveryConfiguration.START_TLS, "true")
@@ -783,7 +783,7 @@ public class RemoteDeliveryConfigurationTest {
 
 
         assertThat(properties)
-            .containsOnly(MapEntry.entry("mail.smtp.ssl.enable", "true"),
+            .containsOnly(MapEntry.entry("mail.smtp.ssl.enable", "false"),
                 MapEntry.entry("mail.smtp.sendpartial", "true"),
                 MapEntry.entry("mail.smtp.ehlo", "true"),
                 MapEntry.entry("mail.smtp.connectiontimeout", 
String.valueOf(connectionTimeout)),
@@ -798,7 +798,7 @@ public class RemoteDeliveryConfigurationTest {
         String helo = "domain.com";
         int connectionTimeout = 1856;
         FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
-            .setProperty(RemoteDeliveryConfiguration.SSL_ENABLE, "true")
+            .setProperty(RemoteDeliveryConfiguration.SSL_ENABLE, "false")
             .setProperty(RemoteDeliveryConfiguration.SENDPARTIAL, "true")
             .setProperty(RemoteDeliveryConfiguration.CONNECTIONTIMEOUT, 
String.valueOf(connectionTimeout))
             .setProperty(RemoteDeliveryConfiguration.START_TLS, "true")
@@ -812,7 +812,7 @@ public class RemoteDeliveryConfigurationTest {
 
 
         assertThat(properties)
-            .containsOnly(MapEntry.entry("mail.smtp.ssl.enable", "true"),
+            .containsOnly(MapEntry.entry("mail.smtp.ssl.enable", "false"),
                 MapEntry.entry("mail.smtp.sendpartial", "true"),
                 MapEntry.entry("mail.smtp.ehlo", "true"),
                 MapEntry.entry("mail.smtp.connectiontimeout", 
String.valueOf(connectionTimeout)),
@@ -822,4 +822,52 @@ public class RemoteDeliveryConfigurationTest {
                 MapEntry.entry("mail.smtp.starttls.enable", "true"),
                 MapEntry.entry("mail.smtp.auth", "true"));
     }
+
+    @Test
+    void 
createFinalJavaxPropertiesWithSSLShouldProvidePropertiesWithMinimalConfiguration()
 {
+        String helo = "domain.com";
+        FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+            .setProperty(RemoteDeliveryConfiguration.SSL_ENABLE, "true")
+            .setProperty(RemoteDeliveryConfiguration.HELO_NAME, helo)
+            .build();
+
+        Properties properties = new RemoteDeliveryConfiguration(mailetConfig, 
mock(DomainList.class)).createFinalJavaxPropertiesWithSSL();
+
+
+        assertThat(properties)
+            .containsOnly(MapEntry.entry("mail.smtps.ssl.enable", "true"),
+                MapEntry.entry("mail.smtps.sendpartial", "false"),
+                MapEntry.entry("mail.smtps.ehlo", "true"),
+                MapEntry.entry("mail.smtps.connectiontimeout", "60000"),
+                MapEntry.entry("mail.smtps.localhost", helo),
+                MapEntry.entry("mail.smtps.timeout", "180000"),
+                MapEntry.entry("mail.debug", "false"),
+                MapEntry.entry("mail.smtps.starttls.enable", "false"));
+    }
+
+    @Test
+    void 
createFinalJavaxPropertiesWithSSLShouldProvidePropertiesWithFullConfigurationWithoutGateway()
 {
+        String helo = "domain.com";
+        int connectionTimeout = 1856;
+        FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+            .setProperty(RemoteDeliveryConfiguration.SSL_ENABLE, "true")
+            .setProperty(RemoteDeliveryConfiguration.SENDPARTIAL, "true")
+            .setProperty(RemoteDeliveryConfiguration.CONNECTIONTIMEOUT, 
String.valueOf(connectionTimeout))
+            .setProperty(RemoteDeliveryConfiguration.START_TLS, "false")
+            .setProperty(RemoteDeliveryConfiguration.HELO_NAME, helo)
+            .build();
+
+        Properties properties = new RemoteDeliveryConfiguration(mailetConfig, 
mock(DomainList.class)).createFinalJavaxPropertiesWithSSL();
+
+
+        assertThat(properties)
+            .containsOnly(MapEntry.entry("mail.smtps.ssl.enable", "true"),
+                MapEntry.entry("mail.smtps.sendpartial", "true"),
+                MapEntry.entry("mail.smtps.ehlo", "true"),
+                MapEntry.entry("mail.smtps.connectiontimeout", 
String.valueOf(connectionTimeout)),
+                MapEntry.entry("mail.smtps.localhost", helo),
+                MapEntry.entry("mail.smtps.timeout", "180000"),
+                MapEntry.entry("mail.debug", "false"),
+                MapEntry.entry("mail.smtps.starttls.enable", "false"));
+    }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to