@@ -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 ) {
0 commit comments