From b0101590b48dbdd05cea9139bd8e15789f0c65b1 Mon Sep 17 00:00:00 2001 From: bbottema Date: Fri, 22 Jul 2016 20:07:30 +0200 Subject: [PATCH] #48 programmatic support for trusting hosts --- .../org/simplejavamail/mailer/Mailer.java | 21 +++++++- .../internal/mailsender/MailSender.java | 30 +++++++++++ .../internal/mailsender/MailSenderTest.java | 50 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/simplejavamail/mailer/internal/mailsender/MailSenderTest.java diff --git a/src/main/java/org/simplejavamail/mailer/Mailer.java b/src/main/java/org/simplejavamail/mailer/Mailer.java index fbe6e6e00..6d31c303d 100644 --- a/src/main/java/org/simplejavamail/mailer/Mailer.java +++ b/src/main/java/org/simplejavamail/mailer/Mailer.java @@ -250,7 +250,7 @@ public Session getSession() { } /** - * Actually sets {@link Session#setDebug(boolean)} so that it generates debug information. To get more information out of the underlying JavaMail + * Calls {@link Session#setDebug(boolean)} so that it generates debug information. To get more information out of the underlying JavaMail * framework or out of Simple Java Mail, increase logging config of your chosen logging framework (examples here). * @@ -261,6 +261,25 @@ public void setDebug(final boolean debug) { mailSender.setDebug(debug); } + /** + * Configures the current session to trust all hosts and don't validate any SSL keys. The property "mail.smtp.ssl.trust" is set to "*". + *

+ * Refer to https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html#mail.smtp.ssl.trust + */ + public void trustAllSSLHosts(boolean trustAllHosts) { + mailSender.trustAllHosts(trustAllHosts); + } + + /** + * Configures the current session to white list all provided hosts and don't validate SSL keys for them. The property "mail.smtp.ssl.trust" is set + * to a comma separated list. + *

+ * Refer to https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html#mail.smtp.ssl.trust + */ + public void trustSSLHosts(String... hosts) { + mailSender.trustHosts(hosts); + } + /** * Copies all property entries into the {@link Session} using {@link Session#getProperties()}. * diff --git a/src/main/java/org/simplejavamail/mailer/internal/mailsender/MailSender.java b/src/main/java/org/simplejavamail/mailer/internal/mailsender/MailSender.java index 187d24454..211f696a2 100644 --- a/src/main/java/org/simplejavamail/mailer/internal/mailsender/MailSender.java +++ b/src/main/java/org/simplejavamail/mailer/internal/mailsender/MailSender.java @@ -246,6 +246,35 @@ public void setDebug(final boolean debug) { session.setDebug(debug); } + /** + * Configures the current session to trust all hosts and don't validate any SSL keys. The property "mail.smtp.ssl.trust" is set to "*". + *

+ * Refer to https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html#mail.smtp.ssl.trust + */ + public void trustAllHosts(boolean trustAllHosts) { + session.getProperties().remove("mail.smtp.ssl.trust"); + if (trustAllHosts) { + session.getProperties().setProperty("mail.smtp.ssl.trust", "*"); + } + } + + /** + * Configures the current session to white list all provided hosts and don't validate SSL keys for them. The property "mail.smtp.ssl.trust" is set + * to a comma separated list. + *

+ * Refer to https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html#mail.smtp.ssl.trust + */ + public void trustHosts(String... hosts) { + trustAllHosts(false); + if (hosts.length > 0) { + StringBuilder builder = new StringBuilder(hosts[0]); + for (int i = 1; i < hosts.length; i++) { + builder.append(",").append(hosts[i]); + } + session.getProperties().setProperty("mail.smtp.ssl.trust", builder.toString()); + } + } + /** * @param properties Properties which will be added to the current {@link Session} instance. */ @@ -267,4 +296,5 @@ public Session getSession() { public synchronized void setThreadPoolSize(int threadPoolSize) { this.threadPoolSize = threadPoolSize; } + } diff --git a/src/test/java/org/simplejavamail/mailer/internal/mailsender/MailSenderTest.java b/src/test/java/org/simplejavamail/mailer/internal/mailsender/MailSenderTest.java new file mode 100644 index 000000000..cae203c62 --- /dev/null +++ b/src/test/java/org/simplejavamail/mailer/internal/mailsender/MailSenderTest.java @@ -0,0 +1,50 @@ +package org.simplejavamail.mailer.internal.mailsender; + +import org.junit.Before; +import org.junit.Test; + +import javax.mail.Session; +import java.util.Properties; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MailSenderTest { + + private Session session; + private MailSender mailSender; + + @Before + public void setup() { + session = Session.getDefaultInstance(new Properties()); + mailSender = new MailSender(session, null, null); + } + + @Test + public void setDebug() { + mailSender.setDebug(true); + assertThat(session.getDebug()).isTrue(); + mailSender.setDebug(false); + assertThat(session.getDebug()).isFalse(); + } + + @Test + public void trustAllHosts() { + mailSender.trustAllHosts(true); + assertThat(session.getProperties().getProperty("mail.smtp.ssl.trust")).isEqualTo("*"); + mailSender.trustAllHosts(false); + assertThat(session.getProperties().getProperty("mail.smtp.ssl.trust")).isNull(); + } + + @Test + public void trustHosts(){ + mailSender.trustHosts(); + assertThat(session.getProperties().getProperty("mail.smtp.ssl.trust")).isNull(); + mailSender.trustHosts("a"); + assertThat(session.getProperties().getProperty("mail.smtp.ssl.trust")).isEqualTo("a"); + mailSender.trustHosts("a", "b"); + assertThat(session.getProperties().getProperty("mail.smtp.ssl.trust")).isEqualTo("a,b"); + mailSender.trustHosts("a", "b", "c"); + assertThat(session.getProperties().getProperty("mail.smtp.ssl.trust")).isEqualTo("a,b,c"); + } + +} \ No newline at end of file