Skip to content

Commit

Permalink
#48 programmatic support for trusting hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
bbottema committed Jul 22, 2016
1 parent a466d26 commit b010159
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/main/java/org/simplejavamail/mailer/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a
* href="http://www.simplejavamail.org/#/proxy">here</a>).
*
Expand All @@ -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 "*".
* <p>
* 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.
* <p>
* 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()}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "*".
* <p>
* 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.
* <p>
* 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.
*/
Expand All @@ -267,4 +296,5 @@ public Session getSession() {
public synchronized void setThreadPoolSize(int threadPoolSize) {
this.threadPoolSize = threadPoolSize;
}

}
Original file line number Diff line number Diff line change
@@ -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");
}

}

0 comments on commit b010159

Please sign in to comment.