Skip to content

Commit 93a2125

Browse files
committed
#435, #436: Added MailerBuilder API entries for .withCustomMailer() and .withTransportModeLoggingOnly() so that SMTP server config can be completely omitted
1 parent 946cd67 commit 93a2125

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/MailerBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import jakarta.mail.Session;
44
import org.jetbrains.annotations.NotNull;
55
import org.jetbrains.annotations.Nullable;
6+
import org.simplejavamail.api.mailer.CustomMailer;
67
import org.simplejavamail.api.mailer.Mailer;
78
import org.simplejavamail.api.mailer.MailerRegularBuilder;
89
import org.simplejavamail.api.mailer.config.TransportStrategy;
@@ -107,6 +108,22 @@ public static MailerRegularBuilderImpl withDebugLogging(Boolean debugLogging) {
107108
return new MailerRegularBuilderImpl().withDebugLogging(debugLogging);
108109
}
109110

111+
/**
112+
* Delegates to {@link MailerRegularBuilder#withCustomMailer(CustomMailer)}.
113+
*/
114+
@SuppressWarnings("deprecation")
115+
public static MailerRegularBuilderImpl withCustomMailer(final CustomMailer customMailer) {
116+
return new MailerRegularBuilderImpl().withCustomMailer(customMailer);
117+
}
118+
119+
/**
120+
* Delegates to {@link MailerRegularBuilder#withTransportModeLoggingOnly(Boolean)}.
121+
*/
122+
@SuppressWarnings("deprecation")
123+
public static MailerRegularBuilderImpl withTransportModeLoggingOnly() {
124+
return new MailerRegularBuilderImpl().withTransportModeLoggingOnly(true);
125+
}
126+
110127
/**
111128
* Shortcuts to {@link MailerRegularBuilder#buildMailer()}. This means that none of the builder methods are used and the configuration completely
112129
* depends on defaults being configured from property file ("simplejavamail.properties") on the classpath or through programmatic defaults.

modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/internal/MailerImpl.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public class MailerImpl implements Mailer {
118118
this.emailGovernance = emailGovernance;
119119
this.proxyConfig = proxyConfig;
120120
if (session == null) {
121-
session = createMailSession(checkNonEmptyArgument(serverConfig, "serverConfig"), checkNonEmptyArgument(transportStrategy, "transportStrategy"));
121+
session = createMailSession(serverConfig, checkNonEmptyArgument(transportStrategy, "transportStrategy"));
122122
}
123123
this.session = session;
124124
this.operationalConfig = operationalConfig;
@@ -151,40 +151,42 @@ public class MailerImpl implements Mailer {
151151
* @see TransportStrategy#propertyNameAuthenticate()
152152
*/
153153
@NotNull
154-
public static Session createMailSession(@NotNull final ServerConfig serverConfig, @NotNull final TransportStrategy transportStrategy) {
154+
public static Session createMailSession(@Nullable final ServerConfig serverConfig, @NotNull final TransportStrategy transportStrategy) {
155155
final Properties props = transportStrategy.generateProperties();
156-
props.put(transportStrategy.propertyNameHost(), serverConfig.getHost());
157-
props.put(transportStrategy.propertyNamePort(), String.valueOf(serverConfig.getPort()));
158-
159-
if (serverConfig.getUsername() != null) {
160-
props.put(transportStrategy.propertyNameUsername(), serverConfig.getUsername());
161-
}
162-
// https://archive.ph/VkrwH (https://www.tutorialspoint.com/javamail_api/javamail_api_smtp_servers.htm)
163-
if (serverConfig.getCustomSSLFactoryInstance() != null) {
164-
props.put("mail.smtp.ssl.socketFactory", serverConfig.getCustomSSLFactoryInstance());
165-
} else if (serverConfig.getCustomSSLFactoryClass() != null) {
166-
props.put("mail.smtp.ssl.socketFactory.class", serverConfig.getCustomSSLFactoryClass());
167-
}
156+
168157
if (ConfigLoader.hasProperty(EXTRA_PROPERTIES)) {
169158
props.putAll(ConfigLoader.getProperty(EXTRA_PROPERTIES));
170159
}
171160

172-
if (transportStrategy == TransportStrategy.SMTP_OAUTH2 && serverConfig.getPassword() == null) {
173-
throw new MailerException(MailerException.MISSING_OAUTH2_TOKEN);
174-
}
161+
if (serverConfig != null) {
162+
props.put(transportStrategy.propertyNameHost(), serverConfig.getHost());
163+
props.put(transportStrategy.propertyNamePort(), String.valueOf(serverConfig.getPort()));
164+
if (serverConfig.getUsername() != null) {
165+
props.put(transportStrategy.propertyNameUsername(), serverConfig.getUsername());
166+
}
167+
// https://archive.ph/VkrwH (https://www.tutorialspoint.com/javamail_api/javamail_api_smtp_servers.htm)
168+
if (serverConfig.getCustomSSLFactoryInstance() != null) {
169+
props.put("mail.smtp.ssl.socketFactory", serverConfig.getCustomSSLFactoryInstance());
170+
} else if (serverConfig.getCustomSSLFactoryClass() != null) {
171+
props.put("mail.smtp.ssl.socketFactory.class", serverConfig.getCustomSSLFactoryClass());
172+
}
175173

176-
if (serverConfig.getPassword() != null) {
177-
if (transportStrategy != SMTP_OAUTH2) {
178-
props.put(transportStrategy.propertyNameAuthenticate(), "true");
179-
return Session.getInstance(props, new SmtpAuthenticator(serverConfig));
180-
} else {
181-
// props.put(transportStrategy.propertyNameAuthenticate(), "false");
182-
props.put(TransportStrategy.OAUTH2_TOKEN_PROPERTY, serverConfig.getPassword());
183-
return Session.getInstance(props);
174+
if (transportStrategy == TransportStrategy.SMTP_OAUTH2 && serverConfig.getPassword() == null) {
175+
throw new MailerException(MailerException.MISSING_OAUTH2_TOKEN);
176+
}
177+
178+
if (serverConfig.getPassword() != null) {
179+
if (transportStrategy != SMTP_OAUTH2) {
180+
props.put(transportStrategy.propertyNameAuthenticate(), "true");
181+
return Session.getInstance(props, new SmtpAuthenticator(serverConfig));
182+
} else {
183+
// props.put(transportStrategy.propertyNameAuthenticate(), "false");
184+
props.put(TransportStrategy.OAUTH2_TOKEN_PROPERTY, serverConfig.getPassword());
185+
return Session.getInstance(props);
186+
}
184187
}
185-
} else {
186-
return Session.getInstance(props);
187188
}
189+
return Session.getInstance(props);
188190
}
189191

190192
static private void initSession(@NotNull final Session session, @NotNull OperationalConfig operationalConfig, @NotNull EmailGovernance emailGovernance, @Nullable final TransportStrategy transportStrategy) {

modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/internal/MailerRegularBuilderImpl.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.simplejavamail.mailer.internal;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.jetbrains.annotations.NotNull;
45
import org.jetbrains.annotations.Nullable;
56
import org.simplejavamail.api.mailer.Mailer;
@@ -25,6 +26,7 @@
2526
/**
2627
* @see MailerRegularBuilder
2728
*/
29+
@Slf4j
2830
public class MailerRegularBuilderImpl extends MailerGenericBuilderImpl<MailerRegularBuilderImpl> implements MailerRegularBuilder<MailerRegularBuilderImpl> {
2931

3032
/**
@@ -198,14 +200,16 @@ public Mailer buildMailer() {
198200
/**
199201
* For internal use.
200202
*/
203+
@Nullable
201204
ServerConfig buildServerConfig() {
202-
vallidateServerConfig();
203-
final int serverPort = ofNullable(port).orElse(transportStrategy.getDefaultServerPort());
204-
return new ServerConfigImpl(verifyNonnullOrEmpty(getHost()), serverPort, username, password, customSSLFactory, customSSLFactoryInstance);
205-
}
206-
207-
private void vallidateServerConfig() {
208-
checkArgumentNotEmpty(host, "SMTP server host missing");
205+
if (!isTransportModeLoggingOnly() && getCustomMailer() == null) {
206+
checkArgumentNotEmpty(host, "SMTP server host missing");
207+
final int serverPort = ofNullable(port).orElse(transportStrategy.getDefaultServerPort());
208+
return new ServerConfigImpl(verifyNonnullOrEmpty(getHost()), serverPort, username, password, customSSLFactory, customSSLFactoryInstance);
209+
} else if (getCustomMailer() != null && host != null) {
210+
log.warn("Both custom mailer and SMTP server configured, ignoring server configuration");
211+
}
212+
return null;
209213
}
210214

211215
/**

0 commit comments

Comments
 (0)