What is the difference between Session.getDefaultInstance(props, authenticator)
and getInstance(props, authenticator)
? In general, when will you choos
FAQ says: https://javaee.github.io/javamail/FAQ#getdefaultinstance
Q: When should I use
Session.getDefaultInstance
and when should I useSession.getInstance
?A: Almost all code should use
Session.getInstance
. TheSession.getDefaultInstance
method creates a new Session the first time it's called, using the Properties that are passed. Subsequent calls will return that original Session and ignore any Properties you pass in. If you want to create different Sessions with different properties, Session.getDefaultInstance won't do that. If some other code in the same JVM (e.g., in the same app server) has already created the default Session with their properties, you may end up using their Session and your properties will be ignored. This often explains why your property settings seem to be ignored. Always useSession.getInstance
to avoid this problem.