Posts tagged ‘mail’

HOWTO: configure Tomcat to send mails, using javamail

This article shows step by step a way to configure Apache Tomcat J2EE server to send emails.

You will need first a working Tomcat server of course, and a mail server. You can have it on your own machine, just like me, so the smtp server would be « localhost », or on another machine from which you can use smtp, for instance your Internet Service Provider smtp (generally

Step 1 : Install the right libraries:

Sending mails with tomcat require 2 librairie: the javamail API, and the Java Activation Framework (jaf).
Depending on your JDK /JRE version, you will need to place 1 or 2 files in your /catalina_home/commons/libs/ folder.
If you have a 1.6 java version (check with java -version) you will need only the mail.jar otherwise, you will need both mail.jar and activation.jar.

Important: you must not include these two files into your webapp, or you will get an almost wired error:
java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session

Let’s download the files:

With JDK / JRE 1.6 it’s the only one you will need. With other versions, take also this file: java activation framework

Unzip these file (if needed, on a Debian system for instance, download the zip package: apt-get unzip), and move it to the appropriate catalina folder (don’t forget to stop tomat service before).

mv /home/your_username/javamail-1.4.1/mail.jar /your_tomcat_home/common/lib/

The same applies to the activaction.jar if needed.

2. Configure your webapp:

2 step to configure your webapp. First, edit the conext.xml, and declare you mail ressource to make it available:

<Resource auth="Container""localhost" name="mail/NomDeLaRessource" type="javax.mail.Session"/>

Next, edit your web.xml file and declare your webapp using your mail resource:

<description>Votre description </description>

3. Write the code

To test your configuration, here is a working sample code:

Session session = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup(« java:comp/env »);
session = (Session) envCtx.lookup(« mail/NomDeLaRessource »);

} catch (Exception ex) {
System.out.println(« lookup error »);
System.out.println( ex.getMessage());

Here is the code which actually send the mail:

Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(expediteur);
InternetAddress to[] = new InternetAddress[1];
to[0] = new InternetAddress(destinataire);
message.setRecipients(Message.RecipientType.TO, to);
message.setContent(corps, "text/html;charset=UTF-8");
} catch (AddressException ex) {
System.out.println( ex.getMessage());
} catch (MessagingException ex) {
System.out.println( ex.getMessage());

The most convenient is to create a hepler method, with receiver adress, subject, body, etc…but that’s your work 🙂

Thanks goes to tomcat mailing list people, especially Chuck and Martin.

Références :


26 septembre 2008 at 9 h 27 min 10 commentaires