Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
basinilya opened this issue Jul 4, 2021 · 2 comments
Closed

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

basinilya opened this issue Jul 4, 2021 · 2 comments

Comments

@basinilya
Copy link

basinilya commented Jul 4, 2021

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 [email protected] --email:to X [email protected] --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:
<[email protected]>
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
@bbottema
Copy link
Owner

bbottema commented Jul 6, 2021

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:

@bbottema
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants