Threadsafety in Javamail Threadsafety in Javamail multithreading multithreading

Threadsafety in Javamail


Admittedly the thread safety rules for JavaMail are not well documented, but hopefully they mostly match what you would expect.

Multiple threads can use a Session.

Since a Transport represents a connection to a mail server, and only a single thread can use the connection at a time, a Transport will synchronize access from multiple threads to maintain thread safety, but you'll really only want to use it from a single thread.

Similarly, a Store can be used by multiple threads, but access to the underlying connection will be synchronized and single threaded.

A Message should only be modified by a single thread at a time, but multiple threads should be able to read a message safely (although it's not clear why you would want to do that).

Hope that helps...


The javamail dispatcher threads doesn't seem to timeout if the server doesn't respond in time. this leads to locking on all available threads.

Tested this behavior with both 1.4.3 & 1.4.5.