djechlin
djechlin

Reputation: 60758

Threadsafety in Javamail

I'm researching whether Javamail is threadsafe, in particular in a situation with many sessions corresponding to different users, several SMTP servers and the use of creating MIME messages and use of transport.sendMessage method. I know Javamail is oriented toward desktop-use which makes me suspect it may not have been built with threading in mind, and am wondering if anyone has such experience.

Upvotes: 28

Views: 10203

Answers (2)

Bill Shannon
Bill Shannon

Reputation: 29971

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).

Upvotes: 78

Nirmala
Nirmala

Reputation: 21

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.

Upvotes: 2

Related Questions