O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

20.1. Placing the Email-Related Classes on your Classpath

Problem

You want to use the javax.mail and related Java packages to handle email in a servlet.

Solution

Download the ZIP files containing the mail.jar and activation.jar archives. Add these JAR files to a shared directory for JAR files whose contents are loaded by the web container. If this directory type is not available, add the mail.jar and activation.jar files to the WEB-INF/lib directory of your web application.

Discussion

If your classpath for compiling servlets already includes the JAR files made available by your web container (such as the JAR files in Tomcat's common/lib directory), test if an email-related servlet such as Example 20-1 compiles successfully. If the compiler reports that the packages javax.mail and javax.mail.internet do not exist, you must add the proper JAR files to your classpath.

Tip

See Recipe 4.3 on using Ant to include Tomcat's JAR files in your classpath.

Download the mail.jar component from http://java.sun.com/products/javamail/. The downloaded file is a ZIP archive containing the mail.jar archive. This file includes the required packages for handling email in a servlet, such as javax.mail and javax.mail.internet.

Then download the JAF from http://java.sun.com/products/javabeans/glasgow/jaf.html. Servlets can use these classes, as part of the javax.activation package, to handle the different data types that can be transferred with email messages, such as file attachments.

Tip

You can handle basic ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required