background image

Running the confirmer Example

<< Class-Based Injection | Creating a Mail Session >>
<< Class-Based Injection | Creating a Mail Session >>

Running the confirmer Example

Like a database connection, a mail session is a resource. In the Application Server, a mail session
is called a JavaMail resource. The resource is injected into the class using @Resource and
specifying the JNDI name of the resource. The type of the session field is
javax.mail.Session
.
@Resource(name=
"mail/myMailSession")
private Session session;
After calling several set methods on the Message object, sendNotice invokes the send method
of the javax.mail.Transport class to send the message. The source code for the sendNotice
method follows.
public void sendNotice(String recipient) {
try {
Message message = new MimeMessage(session);
message.setFrom();
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient, false));
message.setSubject(
"Test Message from ConfirmerBean");
DateFormat dateFormatter = DateFormat
.getDateTimeInstance(DateFormat.LONG,
DateFormat.SHORT);
Date timeStamp = new Date();
String messageText =
"Thank you for your order." + '\n'
+
"We received your order on "
+ dateFormatter.format(timeStamp) +
".";
message.setText(messageText);
message.setHeader(
"X-Mailer", mailer);
message.setSentDate(timeStamp);
// Send message
Transport.send(message);
logger.info(
"Mail sent to " + recipient + ".");
} catch (MessagingException ex) {
ex.printStackTrace();
logger.info(
"Error in ConfirmerBean for " + recipient);
}
}
Running the confirmer Example Application
To run the confirmer example, follow these steps, as described in the following sections:
1. Create a mail session in the Admin Console.
2. Build the example.
The confirmer Example Application
Chapter 34 · Resource Connections
1015