From 4ed91be186bcfbea3f61e1e4f0b516cd82ef3bec Mon Sep 17 00:00:00 2001 From: "Robert J. Macomber" Date: Fri, 3 Mar 2023 13:27:08 -0800 Subject: [PATCH] Try to rework the mail setup There's still a weird thing where it has a single string for both setting the FROM for the message and the SMTP username, which might also need to change, but now it sets mail.smtp _and_ mail.smtps properties as relevant to whether or not it's planning to use SSL, and only does auth if a password is provided. --- .../java/com/socrata/datasync/SMTPMailer.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/socrata/datasync/SMTPMailer.java b/src/main/java/com/socrata/datasync/SMTPMailer.java index a7a8d4f8..edf9d76c 100644 --- a/src/main/java/com/socrata/datasync/SMTPMailer.java +++ b/src/main/java/com/socrata/datasync/SMTPMailer.java @@ -62,19 +62,18 @@ public static void send(String recipientEmail, String ccEmail, String title, Str // Get a Properties object Properties props = System.getProperties(); - props.setProperty("mail.smtps.host", userPrefs.getOutgoingMailServer()); - props.setProperty("mail.smtp.socketFactory.fallback", "false"); - props.setProperty("mail.smtp.port", userPrefs.getSmtpPort()); String sslPort = userPrefs.getSslPort(); - boolean useSSL = !(sslPort.equals("")); + boolean hasPwd = userPrefs.getSmtpPassword() != null && !userPrefs.getSmtpPassword().isEmpty(); if(useSSL) { - - props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); - props.setProperty("mail.smtp.socketFactory.port", sslPort); - props.setProperty("mail.smtps.auth", "true"); + props.setProperty("mail.smtps.host", userPrefs.getOutgoingMailServer()); + props.setProperty("mail.smtps.socketFactory.fallback", "false"); + props.setProperty("mail.smtps.port", sslPort); + props.setProperty("mail.smtps.socketFactory.class", SSL_FACTORY); + props.setProperty("mail.smtps.socketFactory.port", sslPort); + props.setProperty("mail.smtps.auth", hasPwd ? "true" : "false"); /* If set to false, the QUIT command is sent and the connection is immediately closed. If set to true (the default), causes the transport to wait for the response to the QUIT command. @@ -83,9 +82,16 @@ to true (the default), causes the transport to wait for the response to the QUIT http://forum.java.sun.com/thread.jspa?threadID=5205249 smtpsend.java - demo program from javamail */ - props.put("mail.smtps.quitwait", "false"); + props.setProperty("mail.smtps.quitwait", "false"); + } else { + props.setProperty("mail.smtp.host", userPrefs.getOutgoingMailServer()); + props.setProperty("mail.smtp.socketFactory.fallback", "false"); + props.setProperty("mail.smtp.port", userPrefs.getSmtpPort()); + props.setProperty("mail.smtp.auth", hasPwd ? "true" : "false"); + props.setProperty("mail.smtp.quitwait", "false"); } + Session session = Session.getInstance(props, null); // -- Create a new message -- @@ -111,12 +117,14 @@ to true (the default), causes the transport to wait for the response to the QUIT msg.setText(message, "utf-8"); msg.setSentDate(new Date()); + SMTPTransport t = (SMTPTransport)session.getTransport(useSSL ? "smtps" : "smtp"); - SMTPTransport t = (SMTPTransport)session.getTransport("smtp"); - if (useSSL) - t = (SMTPTransport)session.getTransport("smtps"); + if(useSSL || hasPwd) { + t.connect(userPrefs.getOutgoingMailServer(), userPrefs.getSmtpUsername(), userPrefs.getSmtpPassword()); + } else { + t.connect(); + } - t.connect(userPrefs.getOutgoingMailServer(), userPrefs.getSmtpUsername(), userPrefs.getSmtpPassword()); t.sendMessage(msg, msg.getAllRecipients()); t.close(); }