The confirmer Example Application
The
confirmer
example application demonstrates how to use an injected JavaMail session to send a confirmation email.If you've ever ordered a product from a web site, you've probably received an email confirming your order. The
ConfirmerBean
class demonstrates how to send email from an enterprise bean.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 thesession
field isjavax.mail.Session
.After calling several
set
methods on theMessage
object,sendNotice
invokes thesend
method of thejavax.mail.Transport
class to send the message. The source code for thesendNotice
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:Creating a Mail Session
To create a mail session in the Application Server using the Admin Console, follow these steps:
- Open the URL
http://localhost:4848/asadmin
in a browser.- Select the JavaMail Sessions node.
- Click New.
- Type
mail/myMailSession
in the JNDI Name field.- Type the name of the host running your mail server in the Mail Host field.
- Type the destination email address in the Default User field.
- Type your email address in the Default Return Address field.
- Click OK.
Note that
mail/myMailSession
is listed under the JavaMail Sessions node.Building, Packaging, and Deploying confirmer in NetBeans 5.5
Follow these instructions to build, package, and deploy the
confirmer
example to your Application Server instance using the NetBeans 5.5 IDE.
- In NetBeans 5.5, select File
Open Project.
- In the Open Project dialog, navigate to
<
INSTALL
>/javaeetutorial5/examples/ejb/
.- Select the
confirmer
folder.- Select the Open as Main Project and Open Required Projects checkboxes.
- Click Open Project Folder.
- In the Projects tab, right-click the
confirmer
project and select Deploy Project.This builds and packages the application into
confirmer.ear
, located in<
INSTALL
>/javaeetutorial5/examples/ejb/confirmer/dist
, and deploys this EAR file to your Application Server instance.Building, Packaging, and Deploying confirmer Using Ant
To build and package the
confirmer
example, do the following:To deploy
confirmer.ear
, type the following command in a terminal window:Running the Client in NetBeans 5.5
By default, the client sends a message to
pig.bodine@example.com
, a fictional email address. To change the email address in NetBeans 5.5, do the following:To run the client in NetBeans 5.5, right-click the confirmer project in the Projects pane and select Run Project. You should see the following line when the client has successfully sent the test message:
Running the Client Using Ant
By default, the client sends a message to
pig.bodine@example.com
, a fictional email address. To change the email address, set theapp-client.args
property in<
INSTALL
>/examples/ejb/confirmer/nbproject/project.properties
to the email address to which you'd like the test message sent. For example:To retrieve the client JAR and run the client, enter the following command in a terminal:
You should see the following line when the client has successfully sent the test message:
If you changed the target email address, the test message should arrive in the user's inbox in a few moments.