bbottema/simple-java-mail

cli: can't skip smtp auth with custom smtp server

basinilya opened this issue · 2 comments

With today's master 122293a
Setting the custom smtp host and port. User/password set to empty strings to comply with the CLI arguments syntax. It fails to auth because our SMTP expects no auth. If commented out the authenticator creation the email is successfully sent

org.simplejavamail.mailer.internal.MailerImpl.createMailSession(ServerConfig, TransportStrategy)
-               if (serverConfig.getPassword() != null) {
+               if (false && serverConfig.getPassword() != null) {

The minor issue is that a thread pool remains after main() returns.
The other minor issue is that no success message is printed after sending the email. The last message was: SessionLogger - starting mail with session.

$ ~/cli-module-6.6.1-standalone-cli/bin/sjm send --email:forwarding "C:\progs\outlook-message-parser\src\test\resources\test-messages\attachments.msg" --email:from X basin@acme.com --email:to X basin@acme.com --mailer:withSMTPServer gate1.acme.com 465 "" "" --mailer:trustingAllHosts true --mailer:withTransportStrategy SMTPS
00:11:40 [main] DEBUG SMIMESupport - checking for S/MIME signed / encrypted attachments...
00:11:40 [main] DEBUG ConfigLoader - Property file not found on classpath, skipping config file
00:11:40 [main] TRACE MailerImpl - No proxy set, skipping proxy.
00:11:40 [main] DEBUG MailerHelper - validating email...
00:11:40 [main] DEBUG MailerHelper - ...no problems found
00:11:40 [main] DEBUG SessionLogger - starting mail with session (host: gate1.acme.com, port: 465, username: , authenticate: true, transport: SMTPS)
00:11:41 [main] ERROR AbstractProxyServerSyncingClosure - Failed to send email:
<806572546.3.1625433101312@basin.acme.com>
Exception in thread "main" org.simplejavamail.smtpconnectionpool.TransportHandlingException: Error when trying to open connection to the server, session:
	{mail.smtps.writetimeout=60000, mail.smtps.ssl.checkserveridentity=true, mail.smtps.username=, mail.smtps.ssl.trust=*, mail.transport.protocol=smtps, mail.smtps.timeout=60000, mail.smtps.host=gate1.acme.com, mail.smtps.quitwait=false, simplejavamail.transportstrategy=SMTPS, mail.smtps.connectiontimeout=60000, mail.smtps.port=465, mail.smtps.auth=true}
	at org.simplejavamail.smtpconnectionpool.TransportAllocator.allocate(TransportAllocator.java:52)
	at org.simplejavamail.smtpconnectionpool.TransportAllocator.allocate(TransportAllocator.java:30)
	at org.bbottema.genericobjectpool.GenericObjectPool.claimOrCreateNewObjectIfSpaceLeft(GenericObjectPool.java:164)
	at org.bbottema.genericobjectpool.GenericObjectPool.claimOrCreateOrWaitUntilAvailable(GenericObjectPool.java:149)
	at org.bbottema.genericobjectpool.GenericObjectPool.claim(GenericObjectPool.java:93)
	at org.bbottema.clusteredobjectpool.core.ResourcePool.claim(ResourcePool.java:44)
	at org.bbottema.clusteredobjectpool.core.ResourceClusters.claimResourceFromCluster(ResourceClusters.java:124)
	at org.simplejavamail.internal.batchsupport.BatchSupport.acquireTransport(BatchSupport.java:113)
	at org.simplejavamail.mailer.internal.util.TransportRunner.sendUsingConnectionPool(TransportRunner.java:84)
	at org.simplejavamail.mailer.internal.util.TransportRunner.runOnSessionTransport(TransportRunner.java:72)
	at org.simplejavamail.mailer.internal.util.TransportRunner.sendMessage(TransportRunner.java:48)
	at org.simplejavamail.mailer.internal.SendMailClosure.executeClosure(SendMailClosure.java:82)
	at org.simplejavamail.mailer.internal.AbstractProxyServerSyncingClosure.run(AbstractProxyServerSyncingClosure.java:56)
	at org.simplejavamail.mailer.internal.MailerImpl.sendMail(MailerImpl.java:345)
	at org.simplejavamail.mailer.internal.MailerImpl.sendMail(MailerImpl.java:331)
	at org.simplejavamail.internal.clisupport.CliCommandLineConsumerResultHandler.processCliSend(CliCommandLineConsumerResultHandler.java:55)
	at org.simplejavamail.internal.clisupport.CliCommandLineConsumerResultHandler.processCliResult(CliCommandLineConsumerResultHandler.java:45)
	at org.simplejavamail.internal.clisupport.CliSupport.runCLI(CliSupport.java:69)
	at org.simplejavamail.cli.SimpleJavaMail.main(SimpleJavaMail.java:31)
Caused by: javax.mail.AuthenticationFailedException: 535 5.7.0 authentication failed

	at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:947)
	at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:858)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:762)
	at javax.mail.Service.connect(Service.java:364)
	at javax.mail.Service.connect(Service.java:222)
	at javax.mail.Service.connect(Service.java:171)
	at org.simplejavamail.smtpconnectionpool.TransportAllocator.allocate(TransportAllocator.java:47)
	... 18 more

I think the problem is that from the CLI it's not possible to provide null values; they would be empty strings instead. So in this this case, I think it's best to coalesce blank strings to null:

org.simplejavamail.mailer.internal.MailerRegularBuilderImpl

	/**
	 * @see MailerRegularBuilder#withSMTPServer(String, Integer, String, String)
	 */
	@Override
	public MailerRegularBuilderImpl withSMTPServer(@Nullable final String host, @Nullable final Integer port, @Nullable final String username, @Nullable final String password) {
		return withSMTPServerHost(host)
				.withSMTPServerPort(port)
-				.withSMTPServerUsername(username)
-				.withSMTPServerPassword(password);
+				.withSMTPServerUsername(emptyAsNull(username))
+				.withSMTPServerPassword(emptyAsNull(password));
	}
org.simplejavamail.internal.util.MiscUtil

+	@Nullable
+	public static <T> T emptyAsNull(final @Nullable T value) {
+		return valueNullOrEmpty(value) ? null : value;
+	}

Since you're testing with the master branch, perhaps you can verify this solution with this patch:

I've released this change in 6.7.4. Give it a try! If you're still available of course.