Skip to content

Commit

Permalink
#193: Added default SMTP server port based on the transport strategy …
Browse files Browse the repository at this point in the history
…(SMTP: 25, SMTPS: 465 and SMTP_TLS: 587)
  • Loading branch information
bbottema committed Apr 4, 2019
1 parent fa81e72 commit d98c00f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public interface MailerRegularBuilder<T extends MailerRegularBuilder<?>> extends
* To learn more about the various transport modes, the properties they set and the security
* implications, please refer to the full TransportStrategy<br>
* <a href="www.javadoc.io/page/org.simplejavamail/simple-java-mail/latest/org/simplejavamail/mailer/config/TransportStrategy.html">javadoc</a>.
* <p>
* <strong>Note:</strong> if no server port has been set, a default will be taken based on the transport strategy, since every different
* connection type uses a different default port.
*
* @param transportStrategy The name of the transport strategy to use: {@link TransportStrategy#SMTP}, {@link TransportStrategy#SMTPS} or
* {@link TransportStrategy#SMTP_TLS}. Defaults to {@link TransportStrategy#SMTP}.
Expand Down Expand Up @@ -70,7 +73,8 @@ public interface MailerRegularBuilder<T extends MailerRegularBuilder<?>> extends
T withSMTPServerHost(@Nullable String host);

/**
* Sets the optional SMTP port. Will default to pre-configured property if left empty.
* Sets the optional SMTP port. Will default to pre-configured property if not overridden. If left empty,
* the default will be determined based on the transport strategy.
*
* @param port Optional port number that defaults to pre-configured property if left empty.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public enum TransportStrategy {
* </ul>
*/
SMTP {

/**
* When no server port has been configured either through property or by builder api (Java / CLI),
* port <code>{@value}</code> will be used.
*/
private static final int DEFAULT_SMTP_PORT = 25;

/**
* Defaults to enabled opportunistic TLS behavior ({@link #opportunisticTLS}), in case value was not programmatically set or provided
Expand Down Expand Up @@ -141,7 +147,7 @@ public String propertyNameConnectionTimeout() {
public String propertyNameTimeout() {
return "mail.smtp.timeout";
}

/**
* @return "mail.smtp.writetimeout"
*/
Expand Down Expand Up @@ -174,6 +180,15 @@ public String propertyNameSSLTrust() {
public void setOpportunisticTLS(@Nullable final Boolean opportunisticTLS) {
this.opportunisticTLS = opportunisticTLS;
}

/**
* @return {@value DEFAULT_SMTP_PORT}
* @see #DEFAULT_SMTP_PORT
*/
@Override
public int getDefaultServerPort() {
return DEFAULT_SMTP_PORT;
}
},
/**
* SMTP entirely encapsulated by TLS. Commonly known as SMTPS.
Expand All @@ -200,6 +215,13 @@ public void setOpportunisticTLS(@Nullable final Boolean opportunisticTLS) {
* </ul>
*/
SMTPS {

/**
* When no server port has been configured either through property or by builder api (Java / CLI),
* port <code>{@value}</code> will be used.
*/
private static final int DEFAULT_SMTPS_PORT = 465;

/**
* @see TransportStrategy#SMTPS
*/
Expand Down Expand Up @@ -275,7 +297,7 @@ public String propertyNameConnectionTimeout() {
public String propertyNameTimeout() {
return "mail.smtps.timeout";
}

/**
* @return "mail.smtps.writetimeout"
*/
Expand All @@ -299,6 +321,15 @@ public String propertyNameEnvelopeFrom() {
public String propertyNameSSLTrust() {
return "mail.smtps.ssl.trust";
}

/**
* @return {@value DEFAULT_SMTPS_PORT}
* @see #DEFAULT_SMTPS_PORT
*/
@Override
public int getDefaultServerPort() {
return DEFAULT_SMTPS_PORT;
}
},
/**
* Plaintext SMTP with a mandatory, authenticated STARTTLS upgrade.
Expand All @@ -316,6 +347,13 @@ public String propertyNameSSLTrust() {
* </ul>
*/
SMTP_TLS {

/**
* When no server port has been configured either through property or by builder api (Java / CLI),
* port <code>{@value}</code> will be used.
*/
private static final int DEFAULT_SMTP_TLS_PORT = 587;

/**
* @see TransportStrategy#SMTP_TLS
*/
Expand Down Expand Up @@ -392,7 +430,7 @@ public String propertyNameConnectionTimeout() {
public String propertyNameTimeout() {
return "mail.smtp.timeout";
}

/**
* @return "mail.smtp.writetimeout"
*/
Expand All @@ -416,6 +454,15 @@ public String propertyNameEnvelopeFrom() {
public String propertyNameSSLTrust() {
return "mail.smtp.ssl.trust";
}

/**
* @return {@value DEFAULT_SMTP_TLS_PORT}
* @see #DEFAULT_SMTP_TLS_PORT
*/
@Override
public int getDefaultServerPort() {
return DEFAULT_SMTP_TLS_PORT;
}
};

private static final Logger LOGGER = LoggerFactory.getLogger(TransportStrategy.class);
Expand Down Expand Up @@ -481,6 +528,10 @@ public Properties generateProperties() {
* For internal use only.
*/
public abstract String propertyNameTimeout();
/**
* For internal use only.
*/
public abstract int getDefaultServerPort();

/**
* Determines whether TLS should be attempted for SMTP plain protocol (optional if offered by the SMTP server). If not set and no property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.simplejavamail.api.mailer.config.ServerConfig;
import org.simplejavamail.api.mailer.config.TransportStrategy;
import org.simplejavamail.config.ConfigLoader;
import org.simplejavamail.internal.util.SimpleOptional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -166,14 +167,14 @@ public Mailer buildMailer() {
@SuppressWarnings("deprecation")
ServerConfig buildServerConfig() {
vallidateServerConfig();
return new ServerConfigImpl(assumeNonNull(getHost()), assumeNonNull(getPort()), getUsername(), getPassword());
final int serverPort = SimpleOptional.ofNullable(port).orElse(transportStrategy.getDefaultServerPort());
return new ServerConfigImpl(assumeNonNull(getHost()), serverPort, getUsername(), getPassword());
}

private void vallidateServerConfig() {
checkArgumentNotEmpty(host, "SMTP server host missing");
checkArgumentNotEmpty(port, "SMTP server port missing");
}

/**
* @see MailerRegularBuilder#getHost()
*/
Expand Down

0 comments on commit d98c00f

Please sign in to comment.