diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailConfig.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailConfig.java index 46848cdafac..02bb16f9fdc 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailConfig.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailConfig.java @@ -1,61 +1,64 @@ package ca.uhn.fhir.rest.server.mail; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; public class MailConfig { - private String smtpHostname; - private Integer smtpPort; - private String smtpUsername; - private String smtpPassword; - private boolean smtpUseStartTLS; + private String mySmtpHostname; + private Integer mySmtpPort; + private String mySmtpUsername; + private String mySmtpPassword; + private boolean mySmtpUseStartTLS; public MailConfig() { } public String getSmtpHostname() { - return smtpHostname; + return mySmtpHostname; } public MailConfig setSmtpHostname(String theSmtpHostname) { - smtpHostname = theSmtpHostname; + mySmtpHostname = theSmtpHostname; return this; } public Integer getSmtpPort() { - return smtpPort; + return mySmtpPort; } public MailConfig setSmtpPort(Integer theSmtpPort) { - smtpPort = theSmtpPort; + mySmtpPort = theSmtpPort; return this; } public String getSmtpUsername() { - return smtpUsername; + return mySmtpUsername; } public MailConfig setSmtpUsername(String theSmtpUsername) { - smtpUsername = theSmtpUsername; + // SimpleJavaMail treats empty smtp username as valid username and requires auth + mySmtpUsername = StringUtils.isBlank(theSmtpUsername) ? null : theSmtpUsername; return this; } public String getSmtpPassword() { - return smtpPassword; + return mySmtpPassword; } public MailConfig setSmtpPassword(String theSmtpPassword) { - smtpPassword = theSmtpPassword; + // SimpleJavaMail treats empty smtp password as valid password and requires auth + mySmtpPassword = StringUtils.isBlank(theSmtpPassword) ? null : theSmtpPassword; return this; } public boolean isSmtpUseStartTLS() { - return smtpUseStartTLS; + return mySmtpUseStartTLS; } public MailConfig setSmtpUseStartTLS(boolean theSmtpUseStartTLS) { - smtpUseStartTLS = theSmtpUseStartTLS; + mySmtpUseStartTLS = theSmtpUseStartTLS; return this; } diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailSvc.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailSvc.java index 95e2d4d40bb..5c87c4256dd 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailSvc.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/mail/MailSvc.java @@ -62,6 +62,9 @@ public class MailSvc { @Nonnull private Mailer makeMailer(@Nonnull MailConfig theMailConfig) { + ourLog.info("SMTP Mailer config Hostname:[{}] | Port:[{}] | Username:[{}] | TLS:[{}]", + theMailConfig.getSmtpHostname(), theMailConfig.getSmtpPort(), + theMailConfig.getSmtpUsername(), theMailConfig.isSmtpUseStartTLS()); return MailerBuilder .withSMTPServer( theMailConfig.getSmtpHostname(), diff --git a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/mail/MailConfigTest.java b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/mail/MailConfigTest.java index d66cb711ad3..a05d0b8ee92 100644 --- a/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/mail/MailConfigTest.java +++ b/hapi-fhir-server/src/test/java/ca/uhn/fhir/rest/server/mail/MailConfigTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -86,4 +87,22 @@ public class MailConfigTest { assertNotEquals(fixture, null); } + @Test + public void testSetSmtpUsername() { + // execute & validate + assertEquals("xyz", fixture.setSmtpUsername("xyz").getSmtpUsername()); + assertNull(fixture.setSmtpUsername(null).getSmtpUsername()); + assertNull(fixture.setSmtpUsername("").getSmtpUsername()); + assertNull(fixture.setSmtpUsername(" ").getSmtpUsername()); + } + + @Test + public void testSetSmtpPassword() { + // execute & validate + assertEquals("xyz", fixture.setSmtpPassword("xyz").getSmtpPassword()); + assertNull(fixture.setSmtpPassword(null).getSmtpPassword()); + assertNull(fixture.setSmtpPassword("").getSmtpPassword()); + assertNull(fixture.setSmtpPassword(" ").getSmtpPassword()); + } + }