How to change JavaMail port

Tip for anyone else still having issues, we were using Session.getInstance and the port was still defaulting to 25.

Turns out, we were setting the prop value as a Long when it needs to be a String

It didn't error, warn or log, just defaulted to 25.


This happens because you're using getDefaultInstance() which says:

Get the default Session object. If a default has not yet been setup, a new Session object is created and installed as the default.

And that the Properties argument is "used only if a new Session object is created."

So the first time you invoke getDefaultInstance it uses your specified port. After that, the Session has already been created, and subsequent calls to getDefaultInstance will return that same session, and ignore the changed properties.

Try using Session.getInstance() instead of getDefaultInstance(), which creates a new Session each time, using the supplied properties.

It pays to read the javadocs very carefully.