Download
FAQ History |
API
Search Feedback |
Mail Session Connections
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.
Note: The source code for this example is in this directory:
<INSTALL>/j2eetutorial14/ejb/confirmer/src/
.
In the
sendNotice
method of theConfirmerBean
class, thelookup
method returns aSession
object, which represents a mail session. Like a database connection, a mail session is a resource. In the Application Server, a mail session is called a JavaMail resource. As with any resource, you must link the coded name (mail/TheMailSession
) with a JNDI name. Using theSession
object as an argument, thesendNotice
method creates an emptyMessage
object. After calling severalset
methods on theMessage
object,sendNotice
invokes thesend
method of theTransport
class to send the message on its way. The source code for thesendNotice
method follows.public void sendNotice(String recipient) { try { Context initial = new InitialContext(); Session session = (Session) initial.lookup( "java:comp/env/mail/TheMailSession"); Message msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false)); msg.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) + "."; msg.setText(messageText); msg.setHeader("X-Mailer", mailer); msg.setSentDate(timeStamp); Transport.send(msg); } catch(Exception e) { throw new EJBException(e.getMessage()); } }Running the ConfirmerBean Example
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/MySession
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/MySession
is listed under the JavaMail Sessions node.Deploying the Application
- In
deploytool
, open theConfirmerApp.ear
file, which resides in this directory:
<
INSTALL
>/j2eetutorial14/examples/ejb/provided-ears/
- Verify the resource reference.
- Verify the mapping of the reference to the JNDI name.
- Deploy the
ConfirmerApp
application.- In the Deploy Module dialog box, do the following:
Running the Client
To run the
SavingsAccountClient
program, do the following:To modify this example, see the instructions in Modifying the J2EE Application.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.