Chapter 8. Messaging and Remoting
In early 2001, the sun was shining, but I got a call from another kayaker named Steve Daniel saying that North Grape Creek was runnable, but below flood stage. It had never been run before. It required access through private property, and had to be at a perfect level: high enough to run, and low enough to navigate the many low-water crossings that dot the Texas country side. I wondered how Steve timed it perfectly.
Sending Email Messages
In Chapter 6, you learned how to attach behaviors to a bean. You built an example to write a message to a database whenever an exception was thrown. But it would be much more useful to have an email message sent to you when something breaks. Let's use Spring's email abstraction to do that.
How do I do that?
Spring provides a set of services to help you quickly send email messages. You'll need to use three classes to get the support:
The
SimpleMailMessageclass is a value object that has the mail properties that you'll need, such as subject, body, to, from, and cc properties.The
MailExceptionis thrown when something breaks.The
MailSenderinterface andJavaMailSenderprovide Spring's implementation to send messages. You will, of course, need to configure a mail client.
First, you'll need to configure a simple message. Do that in the context, like in Example 8-1.
Example 8-1. RentABike-servlet.xml
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="to"><value>itsupport@rentabike.com</value></property> ...