Chapter 20. Managing Email in Servlets and JSPs

Introduction

This chapter describes how to manage email in your servlets using the JavaMail and JavaBeans Activation Framework (JAF) APIs. JavaMail provides Java classes for dealing with most aspects of creating, sending, and accessing email. The JAF is a separate API for handling the datatypes and Multipurpose Internet Mail Extension (MIME) types you may encounter when generating email, such as the many different kinds of file attachments. Both of these APIs are a part of the Java 2 Enterprise Edition (J2EE) platform.

JavaMail models an email system with classes that represent mail sessions (the javax.mail.Session class), message stores (the javax.mail.Store class), folders (the javax.mail.Folder class, such as the INBOX folder), email messages (javax.mail.Message), and email addresses (the javax.mail.internet.InternetAddress class) . For example, an email message is similar to a JavaBean, with setter methods to build the various message components (e.g., setFrom( ), setRecipients( ), setSubject( ), etc.).

The following recipes show how to manage basic email messaging using a single servlet, as well as methods for separating the responsibility for emailing and handling HTTP requests into JavaBeans and servlets.

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

Get Java Servlet & JSP Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.