Recipe 20.1 Placing the Email-Related Classes on your Classpath


Recipe 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.

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.

You can handle basic file attachments using the JavaMail API alone (without JAF), as in Recipe 20.5 and Recipe 20.6. See Recipe 20.7 for examples of how to use some of the javax.activation classes to add file attachments to emails.


Add the mail.jar and activation.jar archives to the WEB-INF/lib directory of your web application to make the JavaMail and JAF packages available to a servlet.

See Also

The Sun Microsystems JavaMail API page: http://java.sun.com/products/javamail/; the JAF web page: http://java.sun.com/products/javabeans/glasgow/jaf.html; Recipe 20.2 on sending email from a servlet; Recipe 20.3 on sending email using a JavaBean; Recipe 20.4 covering how to access email in a servlet; Recipe 20.5 on accessing email with a JavaBean; Recipe 20.6 on handling attachments in a servlet; Recipe 20.7 on adding attachments to an email message; Recipe 20.8 on reading an email's headers.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net