The Spring Mail API Structure

Central to the Spring Mail abstraction layer are the MailSender and JavaMailSender interfaces shown in Figure 15-1.

image from book
Figure 15-1: The MailSender and JavaMailSender interfaces

The MailSender interface is intended for implementations that only support simple, plain text e-mails. As you can see, JavaMailSender extends the MailSender interface with methods for constructing and sending MIME messages.

COS MailMessage does not support complex MIME messages, and as a result, the implementation based on MailMessage only includes an implementation of MailSender and can only be used for sending plain text e-mails. When you require something more complex than this, you can use the JavaMail-based implementation, which provides an implementation of the JavaMailSender interface, allowing you to send complex MIME messages.

In general, if you are just sending plain text e-mails, it is better to have your application code to the MailSender interface, which provides an extremely simple API for sending messages. If you need to send more complex e-mails, perhaps with attachments or inline images, then you need to code to the JavaMailSender interface and use the JavaMail implementation of the mail abstraction layer.

Configuring Mail Settings Using Spring

Both the JavaMail and COS MailMessage mail layer implementations provide JavaBean-style properties to allow you to configure mail settings in your Spring configuration file. For the COS implementation, the only parameter you can set is the host name of the SMTP server to use for sending mail, but the JavaMail implementation provides a much wider set of parameters to control the protocol, mail host, host port, and server authentication details. In addition to the JavaBean properties for basic configuration, the JavaMail implementation allows you to provide any of the recognized JavaMail configuration parameters using an instance of Properties and the <properties> tag in your configuration file.

The SimpleMailMessage Class

One of the core classes used by both the COS and JavaMail implementations is SimpleMailMessage. This class encapsulates the details of a basic mail message in a simple JavaBean. Using this class, you can create templates for your messages in your configuration file, specifying details such as the sender address and the subject, and then just modify the appropriate details in your application code. To avoid threading issues, SimpleMailMessage has a copy constructor that accepts another instance of SimpleMailMessage, allowing you to create a sandbox instance of your message that can be modified separately on your thread without affecting the shared instance.



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