Update 2009-09-12: I no longer recommend this library. Please see the comments.
My colleague Allard just pointed me to an old but very useful library: HA-JavaMail.
The email sender that the JVM provides has some serious shortcomings. It does not automatically open a new connection when the connection was closed and you can forward your e-mail to 1 SMTP server only. Furthermore, it is not so fast. HA-JavaMail circumvents these problems by wrapping the JVM implementation.
In this tiny article I explain how you configure HA-JavaMail from Spring. First make sure you have the HA-JavaMail jar, the Spring jar, the JavaMail mail.jar and the JAF activation.jar library on your classpath. The latter two are available by default in full JEE containers.
Convert your mail sender bean declaration from this:
to something like this:
Note that the example adds a second SMTP server that will be used when the first is unreachable. Only a value for mail.transport.host is required. The other values shown here are also the defaults.
That’s it. You now have a much more reliable and faster email service.
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="mail.host" value="localhost"/></bean>
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="javaMailProperties"><value>
mail.transport.host=localhost,mail.example.com
mail.transport.pool-size=1
mail.transport.connect-timeout=0
mail.transport.connect-retry-period=60
mail.transport.sender-strategy=net.sf.hajavamail.SimpleSenderStrategy
</value></property></bean>