HOWTO: configure Tomcat to send mails, using javamail

26 septembre 2008 at 9 h 27 min 10 commentaires

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. Tags: , , , , , .

Configurer Tomcat pour envoyer des mails Crappy loop inside mysql request… Play with mysql procedure.

10 commentaires Add your own

  • 2. Yves  |  5 mars 2010 à 17 h 29 min

    No where near enough information to someone who is not too familiar with tomcat.

    Réponse
  • 3. exxor  |  3 Mai 2010 à 8 h 02 min

    Can you please help me to create a website where in when a user log in i need to send an email to the admin. i have tried with all available codes.
    When i use your code i am unable to create the initial context. i get error at this part.
    please advise and help.

    Réponse
  • 4. anandchakru  |  7 octobre 2010 à 18 h 40 min

    http://www.oracle.com/technetwork/java/index-138643.html

    Is the current location

    Réponse
  • 5. mamun  |  7 février 2011 à 12 h 46 min

    rtrtreter

    Réponse
  • 6. Podlivkin  |  2 juillet 2011 à 18 h 52 min

    This variety of hardware, operating systems, programming languages, frameworks, servers, browsers makes this world really crazy. You do not concentrate on your work. Instead of this you try to find a solution how to make this tool work, how to integrate different tools together, how to configure your development environment, how to make your tiny application send these damn e-mails.

    Where is this file conext.xml located? Ok, the name of this file is conText.xml and it is located in $TOMCAT_HOME/conf/ folder. Then, if you need to specify username, password, SMTP port, you should add the following lines to context.xml:

    But, after restart of tomcat these lines will be eliminated from context.xml. Why? Refer to the documentation (as usual, read, read, read – just do not do your work. Do you really think that I need all that knowledge about how your crappy products work? )

    And last, you write:

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

    If you develop webapp using Apache CXF, these two jars are not included, BUT Apache CXF library is included and it contains geronimo-javamail_xx.jar. So, after deployment you should proceed to the directory $TOMCAT_HOME/webapps/you_app/WEB-INF/lib/ and delete this geronimo-javamail_xx.jar. Restart Tomcat. Test your application. It still doesn’t work.

    Ok. Let’s google further. There should be another lonely soul in this world who might have solved this issue.

    Réponse
  • […] To send emails, you of course need mail.jar (and for older JVMs also activation.jar), here are instructions for Tomcat […]

    Réponse
  • 8. Des  |  14 novembre 2012 à 17 h 22 min

    When trying this from tomcat SSL I get this exception :

    javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 365;

    I did not specify port 365 ??????

    Réponse
  • 9. MymeTime  |  9 janvier 2013 à 13 h 48 min

    Thank you very much for the tutorial.
    🙂

    Réponse
  • 10. Róbert  |  21 mars 2013 à 16 h 12 min

    Nice summary! If you get a chance, please correct conext.xml to context.xml. That would be very nice!

    Réponse

Laisser un commentaire

Trackback this post  |  Subscribe to the comments via RSS Feed