Fixed issue 40603: NPE if email timeout is not set
authorJavier Armendáriz <javier.armendariz@openbravo.com>
Fri, 12 Apr 2019 14:54:40 +0200
changeset 35694 2ccef54104f1
parent 35693 f3ff80a1ad20
child 35695 c442fd2860b4
Fixed issue 40603: NPE if email timeout is not set

Adding another null check to verify not only in the case that the email config
does not exist, but also that the timeout value is empty. In both cases the
default timeout should be used.
src/org/openbravo/erpCommon/utility/poc/EmailManager.java
--- a/src/org/openbravo/erpCommon/utility/poc/EmailManager.java	Fri Apr 12 10:11:29 2019 +0200
+++ b/src/org/openbravo/erpCommon/utility/poc/EmailManager.java	Fri Apr 12 14:54:40 2019 +0200
@@ -9,7 +9,7 @@
  * either express or implied. See the License for the specific language
  * governing rights and limitations under the License. The Original Code is
  * Openbravo ERP. The Initial Developer of the Original Code is Openbravo SLU All
- * portions are Copyright (C) 2001-2018 Openbravo SLU All Rights Reserved.
+ * portions are Copyright (C) 2001-2019 Openbravo SLU All Rights Reserved.
  * Contributor(s): ______________________________________.
  * ***********************************************************************
  */
@@ -64,7 +64,7 @@
   public static void sendEmail(EmailServerConfiguration conf, EmailInfo email) throws Exception {
 
     String decryptedPassword = FormatUtilities.encryptDecrypt(conf.getSmtpServerPassword(), false);
-    Long timeoutMillis = TimeUnit.SECONDS.toMillis(conf.getSmtpConnectionTimeout());
+    Long timeoutMillis = getSmtpConnectionTimeout(conf);
 
     sendEmail(conf.getSmtpServer(), conf.isSMTPAuthentification(), conf.getSmtpServerAccount(),
         decryptedPassword, conf.getSmtpConnectionSecurity(), conf.getSmtpPort().intValue(),
@@ -85,15 +85,19 @@
 
     EmailServerConfiguration configuration = EmailUtils
         .getEmailConfiguration(OBContext.getOBContext().getCurrentOrganization());
-    Long timeoutMillis = (configuration != null)
-        ? TimeUnit.SECONDS.toMillis(configuration.getSmtpConnectionTimeout())
-        : DEFAULT_SMTP_TIMEOUT;
+    Long timeoutMillis = getSmtpConnectionTimeout(configuration);
 
     sendEmail(host, auth, username, password, connSecurity, port, senderAddress, recipientTO,
         recipientCC, recipientBCC, replyTo, subject, content, contentType, attachments, sentDate,
         headerExtras, timeoutMillis.intValue());
   }
 
+  private static Long getSmtpConnectionTimeout(EmailServerConfiguration configuration) {
+    return (configuration != null && configuration.getSmtpConnectionTimeout() != null)
+        ? TimeUnit.SECONDS.toMillis(configuration.getSmtpConnectionTimeout())
+        : DEFAULT_SMTP_TIMEOUT;
+  }
+
   private static void sendEmail(String host, boolean auth, String username, String password,
       String connSecurity, int port, String senderAddress, String recipientTO, String recipientCC,
       String recipientBCC, String replyTo, String subject, String content, String contentType,