Chapter 15: Mail Support in Spring

Overview

For many applications, e-mail is a perfect medium for a wide variety of tasks such as user notification, marketing, and, in some cases, application integration. In the Java world, there is a large selection of e-mail solutions, the most well known of which is JavaMail from Sun. Although JavaMail is exceptionally powerful, it is quite a complex API; as a result, sending a simple e-mail with JavaMail is not a trivial task. Thankfully, Spring provides full support for e- mail through a much-simplified API. The Spring Mail API is fully pluggable, and out of the box, you get two implementations: one that uses JavaMail, and another that uses the MailMessage class written by Jason Hunter as part of the com.oreilly.servlet (COS) package available at www.servlets.com. In this chapter, we are going to examine the Spring e-mail abstraction layer, including both the JavaMail and COS MailMessage implementations.

Spring's e-mail abstraction layer is split into two parts: one for basic mail support and one for more complex mail support. The JavaMail implementation supports both parts, whereas the COS implementation only supports the sending of simple messages. For this reason, much of the discussion in this chapter is focused on the JavaMail implementation. To follow the examples in this chapter, you should familiarize yourself with the JavaMail and MailMessage APIs, because we will not go into great detail about either of these APIs.

In particular, this chapter focuses on the following topics:

The Spring Mail API structure: In this section, we introduce the various interfaces and classes that make up the Spring Mail API along with the two implementations that are provided out of the box. We look at how the mail API is structured for use in a DI-based environment and how this benefits your application.

Sending simple e-mails: In this section, we start off with basic examples for sending plain text e-mail using a completely programmatic approach with both JavaMail and COS. The final example in this section revisits the birthday reminder application from the previous chapter and demonstrates how to use the Spring Mail API in a more declarative fashion.

Sending MIME messages: This section is wholly focused on the JavaMail implementation of the Spring Mail API and shows you how to assemble nontrivial mail messages. In particular, we look at how to construct complex multipart messages with embedded image content, alternative formats, and attachments.

Using Mail with Velocity: In this section, we look at a practical extension to the mail support using the Jakarta Velocity templating engine to simplify message creation. We also demonstrate how you can build a simple framework for creating and sending messages containing alternative formats using Spring Mail and Velocity.

In order to run the examples in this section, you need to download both JavaMail and COS MailMessage. If you downloaded the full Spring distribution with all the dependencies included, you will find all the JAR files there. Otherwise, you can obtain JavaMail along with the JavaBeans Activation Framework, which is required by JavaMail, from http://java.sun.com. You can obtain COS MailMessage from http://servlets.com/cos.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

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