Sending Simple E-Mails

The most basic task when sending e-mail is to send a simple, plain text e-mail. Both the COS and JavaMail implementations support this functionality, and both allow this to be achieved using both programmatic and declarative mechanisms. In the first part of this section, you learn how to create and send messages programmatically, and in the second part, we look at an alternative implementation that uses the declarative approach to configure both the MailSender implementations and the SimpleMailMessage.

Constructing and Sending E-Mail Programmatically

To send mail programmatically, the first task is to create an instance of SimpleMailMessage and then configure it with the appropriate details, such as subject, body text, and recipient address. The next step is to create an instance of MailSender and then set the appropriate configuration details; in particular, you need to set the host address of the mail server.

In Listing 15-1, you can see the SimpleMailSender class, which acts as a base class for the examples in this section.

Listing 15-1: The SimpleMailSender Class

image from book
package com.apress.prospring.ch15.simple;      import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage;      public abstract class SimpleMailSender {          protected abstract MailSender getMailSender();          public final void sendMessage(String to, String text) {         SimpleMailMessage msg = new SimpleMailMessage();         msg.setTo(to);         msg.setSubject("Test Message");         msg.setFrom("test@apress.com");         msg.setText(text);                  MailSender sender = getMailSender();         sender.send(msg);     }     }
image from book

The first thing to notice here is that this class is declared abstract and has an abstract method, getMailSender(). We will create two different base classes: one that returns an instance of CosMailSenderImpl and one that returns JavaMailSenderImpl. In this way, we can reuse the logic in sendMessage(). In the sendMessage() method, we assemble an instance of SimpleMailMessage with content for the subject and body and addresses for the sender and recipient. Once this instance is assembled, we use the instance of MailSender returned by the getMailSender() method to send the message. The MailSender interface includes two send() methods, one that accepts a single instance of SimpleMailMessage and another that accepts an array of SimpleMailMessage objects for sending messages in bulk.

In Listing 15-2, you can see the JavaMailSimpleMailSender class that extends SimpleMailSender to return an instance of JavaMailSenderImpl from the getMailSender() method.

Listing 15-2: The JavaMailSimpleMailSender Class

image from book
package com.apress.prospring.ch15.simple;      import org.springframework.mail.MailSender; import org.springframework.mail.javamail.JavaMailSenderImpl;      public class JavaMailSimpleMailSender extends SimpleMailSender {          protected MailSender getMailSender() {         JavaMailSenderImpl sender = new JavaMailSenderImpl();         sender.setHost("localhost");         return sender;     } }
image from book

Notice that in the getMailSender() method, we create an instance of JavaMailSenderImpl and configure the mail host using the setHost() method. Most likely, you will need to change the mail host address for your environment. Be aware that the setHost() method is not defined on either the MailSender or JavaMailSender interfaces, so you cannot configure the mail host in a implementation-agnostic manner when using this approach. In Listing 15-3, you can see the CosSimpleMailSender class, which shows a simple implementation like that shown in Listing 15-2 but for the CosMailSenderImpl class.

Listing 15-3: The CosSimpleMailSender Class

image from book
package com.apress.prospring.ch15.simple;      import org.springframework.mail.MailSender; import org.springframework.mail.cos.CosMailSenderImpl;      public class CosSimpleMailSender extends SimpleMailSender {          protected MailSender getMailSender() {         CosMailSenderImpl sender = new CosMailSenderImpl();         sender.setHost("localhost");         return sender;     }     }
image from book

Here you can see that aside from the class instance that is created, this implementation is identical to that of the JavaMailSimpleMailSender class. At this point, we have two classes that, using different MailSender implementations, can send a message containing some arbitrary text to a single recipient. Listing 15-4 shows the SimpleMailTest class that tests out the CosSimpleMailSender and JavaMailSimpleMailSender classes.

Listing 15-4: Sending the E-Mails

image from book
package com.apress.prospring.ch15.simple;      public class SimpleMailTest {          private static final String TO = "robh@cakesolutions.net";          private static final String TEXT = "Hello World!";          public static void main(String[] args) {              SimpleMailSender sender1 = new JavaMailSimpleMailSender();         SimpleMailSender sender2 = new CosSimpleMailSender();              sender1.sendMessage(TO, TEXT);         sender2.sendMessage(TO, TEXT);     } }
image from book

Here we are using an instance of JavaMailSimpleMailSender and an instance of CosSimpleMailSender to send two copies of the same message to the same recipient. When you run this example, it takes a few seconds to execute depending on your mail server and the connection speed and it then terminates. In Figure 15-2, you can see the results of this code rendered in our mail client.

image from book
Figure 15-2: The Hello World mail message

As you can see from this example, sending simple, plain text e-mails is extremely straightforward. However, this approach has a big drawback: all configuration data for the MailSender implementations and the SimpleMailMessage objects is contained in the code. In the next example, you will see how to configure the MailSender and SimpleMailMessage in the Spring configuration file.

Constructing E-Mail Declaratively

In the previous section, we alluded to the fact that it is possible to configure a MailSender in your Spring configuration file and to create a template SimpleMailMessage containing the basic details of your mail. In this section, we revisit the birthday reminder example from the previous chapter to add e-mail-based reminders.

The first step in this example is to create a TimerTask implementation that sends reminders to e-mail. This is shown in Listing 15-5.

Listing 15-5: The ReminderTask Class

image from book
package com.apress.prospring.ch15.birthday;      import java.util.TimerTask;      import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage;      public class ReminderTask extends TimerTask {          private SimpleMailMessage defaultMessage;     private MailSender sender;     private String who;              public void setMailMessage(SimpleMailMessage defaultMessage) {         this.defaultMessage = defaultMessage;     }          public void setMailSender(MailSender sender) {         this.sender = sender;     }          public void setWho(String who) {         this.who = who;     }          public void run() {         SimpleMailMessage msg = new SimpleMailMessage(defaultMessage);         msg.setText("Remember! It is " + who + "'s birthday in 7 days.");         sender.send(msg);         System.out.println("Sent reminder for: " + who);     } }
image from book

The first thing you should notice in this code is that we defined two JavaBean properties, mailSender and mailMessage. These allow for instances of MailSender and SimpleMailMessage to be set using DI. In the run() method, we create an instance of SimpleMailMessage using the externally provided instance as a template. Then we set the message text to be the reminder, and we send it using the externally provided MailSender instance. Notice that we haven't specified any configuration properties for the MailSender nor do we specify a subject, sender address, or recipient address for the SimpleMailMessage. These parameters are all set in the configuration file shown in Listing 15-6.

Listing 15-6: Configuring Mail Settings Externally

image from book
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>     <bean             >         <property name="date">             <value>2004-11-29</value>         </property>         <property name="fixedRate">             <value>true</value>         </property>         <property name="timerTask">             <bean >                 <property name="who">                     <value>Mum</value>                 </property>                 <property name="mailSender">                     <ref local="sender"/>                 </property>                 <property name="mailMessage">                     <ref local="mailMessage"/>                 </property>             </bean>         </property>     </bean>          <bean  >         <property name="host">             <value>localhost</value>         </property>     </bean>              <bean  >         <property name="from">             <value>reminders@apress.com</value>         </property>         <property name="to">             <value>robh@cakesolutions.net</value>         </property>         <property name="subject">             <value>Birthday Reminder!!</value>         </property>     </bean>          <bean             >         <property name="scheduledTimerTasks">             <list>                 <ref local="mum"/>             </list>         </property>     </bean> </beans>
image from book

You should recognize much of this configuration code from the previous chapter; the important parts are the mailMessage and sender bean declarations. For the sender bean, we use the JavaMailSenderImpl class and we configure the host property to be localhost, as we did when using the programmatic approach. For the mailMessage bean we have set the to, from, and subject properties. Each of these properties is inherited by the instance of SimpleMailMessage created in the run() method, because we use this instance of SimpleMailMessage as a template.

Listing 15-7 shows a simple driver class that loads the configuration shown in Listing 15-6 and then simply waits.

Listing 15-7: Loading the Mail Configuration

image from book
package com.apress.prospring.ch15.birthday;      import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext;      public class Runner {          public static void main(String[] args) throws Exception {              ApplicationContext ctx = new FileSystemXmlApplicationContext(                 "./ch15/src/conf/birthdayReminder.xml");         System.in.read();     } }
image from book

Until the date in the configuration file is reached, no e-mail is sent, so you might want to change the date to one that is more relevant or change the configuration for the scheduled task to use a different schedule altogether. Figure 15-3 shows a sample message generated by this example.

image from book
Figure 15-3: The birthday reminder e-mail

The declarative approach for configuration is generally preferred to the wholly program- matic approach, because it allows you to externalize fully all configuration, including basic message configuration, and you can take advantage of Dependency Injection (DI) when providing your application components with instances of MailSender and SimpleMailMessage. This is particularly useful because it allows you to swap out one implementation of MailSender for another.



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

Similar book on Amazon

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