Skip to content

Commit

Permalink
Correctly use start TLS for emails
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Jan 2, 2024
1 parent abee49a commit 7c75794
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class BaseMailHostServerConfiguration implements MailHostServerConfigurat

protected int port = 25;
protected Transport transport = Transport.SMTP;
protected boolean startTlsEnabled;

protected String user;
protected String password;
Expand Down Expand Up @@ -52,6 +53,15 @@ public void setTransport(Transport transport) {
this.transport = transport;
}

@Override
public boolean isStartTlsEnabled() {
return startTlsEnabled;
}

public void setStartTlsEnabled(boolean startTlsEnabled) {
this.startTlsEnabled = startTlsEnabled;
}

@Override
public String user() {
return user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public non-sealed interface MailHostServerConfiguration extends MailServerConfig

String password();

boolean isStartTlsEnabled();

enum Transport {
SMTP,
SMTPS,
SMTPS_TLS,
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ protected Session createSession(MailHostServerConfiguration serverConfiguration)
properties.setProperty(MAIL_HOST, host);

MailHostServerConfiguration.Transport transport = serverConfiguration.transport();
properties.setProperty(MAIL_TRANSPORT_STARTTLS_ENABLE, transport == MailHostServerConfiguration.Transport.SMTPS_TLS ? "true" : "false");
properties.setProperty(MAIL_TRANSPORT_STARTTLS_ENABLE, Boolean.toString(serverConfiguration.isStartTlsEnabled()));
properties.setProperty(MAIL_TRANSPORT_STARTTLS_REQUIRED, "false");

properties.setProperty(MAIL_SMTP_SEND_PARTIAL, "false");
Expand All @@ -390,7 +390,7 @@ protected Session createSession(MailHostServerConfiguration serverConfiguration)
properties.setProperty(MAIL_SMTP_AUTH, "true");
}

if (transport == MailHostServerConfiguration.Transport.SMTPS || transport == MailHostServerConfiguration.Transport.SMTPS_TLS) {
if (transport == MailHostServerConfiguration.Transport.SMTPS) {
properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_PORT, String.valueOf(serverConfiguration.port()));
properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_CLASS, "javax.net.ssl.SSLSocketFactory");
properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_FALLBACK, "false");
Expand Down

0 comments on commit 7c75794

Please sign in to comment.