HOWTO: configure Tomcat to send mails, using javamail

septembre 26, 2008

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 smtp.nameofyourISP.com)

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:

javamail-1_4_1.zip

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" mail.smtp.host="localhost" name="mail/NomDeLaRessource" type="javax.mail.Session"/>

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

<resource-ref>
<description>Votre description </description>
<res-ref-name>mail/NomDeLaRessource</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

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.setSubject(sujet);
message.setContent(corps, "text/html;charset=UTF-8");
Transport.send(message);
} 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 : tomcat.apache.org/

Entry Filed under: administration, english publications, java, tutorial. Mots-clefs: , , , , , .

1 Comment Add your own

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Catégories

Articles récents

Mots-clefs

2.0 java blogosphère admin apache configuration CSS debian desactiver desinstaller démons ECMAScript fluxbb forum gpg GWT httpd.conf IDE IM inetd install installer php+mysql inutiles iphone iphoneOS jabber java javamail Javascript kopete mysql ménage netbeans new Function php phpbb privacité pstree punbb remove daemon serveur dédié services services linux sujet tomcat utf8 xinetd

Méta

Articles les plus consultés