Skip to content

Commit

Permalink
#116: provide a connection test based on the current configuration (T…
Browse files Browse the repository at this point in the history
…ransportStrategy and anynomous / authenticated proxy)
  • Loading branch information
bbottema committed Nov 16, 2017
1 parent 692eabf commit cb091f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/simplejavamail/mailer/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,16 @@ public void trustAllSSLHosts(final boolean trustAllHosts) {
public void trustSSLHosts(final String... hosts) {
mailSender.trustHosts(hosts);
}


/**
* Tries to connect to the configured SMTP server, including (authenticated) proxy if set up.
* <p>
* Note: synchronizes on the thread for sending mails so that we don't get into race condition conflicts with emails actually being sent.
*/
public void testConnection() {
mailSender.testConnection();
}

/**
* 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 @@ -15,7 +15,11 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.mail.*;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
Expand Down Expand Up @@ -173,6 +177,7 @@ private static AnonymousSocks5Server configureSessionWithProxy(final ProxyConfig
}
if (effectiveProxyConfig.requiresAuthentication()) {
if (transportStrategy != null) {
// wire anonymous proxy request to our own proxy bridge so we can perform authentication to the actual proxy
sessionProperties.put(transportStrategy.propertyNameSocksHost(), "localhost");
sessionProperties.put(transportStrategy.propertyNameSocksPort(), String.valueOf(effectiveProxyConfig.getProxyBridgePort()));
} else {
Expand Down Expand Up @@ -278,8 +283,7 @@ private void sendMailClosure(@Nonnull final Session session, @Nonnull final Emai

try {
synchronized (this) {
// proxy server is null when not needed
if (proxyServer != null && !proxyServer.isRunning()) {
if (needsAuthenticatedProxy() && !proxyServer.isRunning()) {
LOGGER.trace("starting proxy bridge");
proxyServer.start();
}
Expand Down Expand Up @@ -338,7 +342,7 @@ private synchronized void checkShutDownRunningProcesses() {
// if this thread is the last one finishing
if (smtpRequestsPhaser.getUnarrivedParties() == 0) {
LOGGER.trace("all threads have finished processing");
if (proxyServer != null && proxyServer.isRunning() && !proxyServer.isStopping()) {
if (needsAuthenticatedProxy() && proxyServer.isRunning() && !proxyServer.isStopping()) {
LOGGER.trace("stopping proxy bridge...");
proxyServer.stop();
}
Expand Down Expand Up @@ -412,7 +416,39 @@ public void trustHosts(final String... hosts) {
session.getProperties().setProperty(transportStrategy.propertyNameSSLTrust(), builder.toString());
}
}


/**
* Tries to connect to the configured SMTP server, including (authenticated) proxy if set up.
* <p>
* Note: synchronizes on this mailer instance, so that we don't get into race condition conflicts with emails actually being sent.
*/
public synchronized void testConnection() {
boolean proxyBridgeStartedForTestingConnection = false;

try (Transport transport = session.getTransport()) {
if (needsAuthenticatedProxy() && !proxyServer.isRunning()) {
LOGGER.trace("starting proxy bridge for testing connection");
proxyServer.start();
proxyBridgeStartedForTestingConnection = true;
}
transport.connect(); // actual test
} catch (MessagingException e) {
throw new MailSenderException(MailSenderException.ERROR_CONNECTING_SMTP_SERVER, e);
} finally {
if (proxyBridgeStartedForTestingConnection) {
LOGGER.trace("stopping proxy bridge after connection test");
proxyServer.stop();
}
}
}

/**
* Proxy server is null when not needed. Method is for readability.
*/
private boolean needsAuthenticatedProxy() {
return proxyServer != null;
}

/**
* @param properties Properties which will be added to the current {@link Session} instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class MailSenderException extends MailException {
static final String INVALID_PROXY_SLL_COMBINATION = "Proxy is not supported for SSL connections (this is a limitation by the underlying JavaMail framework)";
static final String GENERIC_ERROR = "Third party error";
static final String INVALID_ENCODING = "Encoding not accepted";
static final String CANNOT_SET_TRUST_WITHOUT_TRANSPORTSTRATEGY =
"Cannot determine the trust properties to set without a provided transport strategy";
static String CANNOT_SET_BOUNCETO_WITHOUT_TRANSPORTSTRATEGY =
"Cannot determine the envelope .from property to set without a provided transport strategy";
static final String CANNOT_SET_TRUST_WITHOUT_TRANSPORTSTRATEGY = "Cannot determine the trust properties to set without a provided transport strategy";
static final String CANNOT_SET_BOUNCETO_WITHOUT_TRANSPORTSTRATEGY = "Cannot determine the envelope .from property to set without a provided transport strategy";
static java.lang.String ERROR_CONNECTING_SMTP_SERVER = "Was unable to connect to SMTP server";

MailSenderException(@SuppressWarnings("SameParameterValue") final String message) {
super(message);
Expand Down

0 comments on commit cb091f1

Please sign in to comment.