When I try to send a Apache Commons HtmlEmail with a specific port set, it works locally on my machine, but in the deployed docker container, the connection is refused because the port setting is ignored and the default port is used.
I cannot share the code of the whole application, which contains an email service written in Kotlin, that is compiled to a jar with other java sources with Maven and run in a docker container based on eclipse temurin 11. The crazy thing is that this is not a problem (with the same container!) on an Ubuntu 18.04 host, but it fails on an Ubuntu 22.04 host.
I tried to reproduce the problem locally, but locally, it worked. The Kotlin code that is basically the same that runs in the app can be run on the Kotlin CLI (needs https://mvnrepository.com/artifact/javax.activation/activation/1.1.1, https://javaee.github.io/javamail/#Download_JavaMail_Release and https://commons.apache.org/proper/commons-email/download_email.cgi):
kotlinc -cp commons-email-1.5.jar:javax.mail.jar:activation-1.1.1.jar
import org.apache.commons.mail.DefaultAuthenticator
import org.apache.commons.mail.HtmlEmail
val email = HtmlEmail()
email.hostName = "smtp.ionos.de"
email.setSmtpPort(587)
email.setAuthenticator(DefaultAuthenticator("[redacted]","[redacted]"))
email.isSSLOnConnect = true
email.setFrom("mail@[redacted]")
email.subject = "Test from ThinkPad"
email.setTextMsg("This is a drill")
email.addTo("[redacted]")
email.send()
I found nearly nothing specific to Apache Commons Mail, and found something on different settings that influence the default behaviour, but a) should they not be set by the framework? and b) this should not be an issue since the thing runs inside a docker container...
Edit: Thank you Robert for clarifying! Also I found this answer https://stackoverflow.com/a/21296375/20756369 and I would have liked to just ask "HOW do I set the server's default settings" but I do not have enough Karma to comment so apparently it is better if I spam an entirely new question.
Edit: I found my mistake
As per the documentation, isSSLOnConnect(true) needs to be used with setSslSmtpPort(String), while no TLS or setStartTLSRequired(true) use the port that is set by setSmtpPort(Int).
The used hosting provider (IONOS) apparently only offers startTLS on port 587, and I have yet to get SSL on port 995 to work, but that is a different topic.
As for why it works on the old on-premise host, but not on the one in the cloud: Some Hosting Providers block port 465 to prevent spammers from using their services.