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 the ConfirmerBean class, the lookup method returns a Session 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 the Session object as an argument, the sendNotice method creates an empty Message object. After calling several set methods on the Message object, sendNotice invokes the send method of the Transport class to send the message on its way. The source code for the sendNotice method follows.

public void sendNotice(String recipient) {

   try {
       Context initial = new InitialContext();
       Session session = 
         (Session) initial.lookup(
       Message msg = new MimeMessage(session);

          InternetAddress.parse(recipient, false));

       msg.setSubject("Test Message from ConfirmerBean");
       DateFormat dateFormatter =
         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.setHeader("X-Mailer", mailer);


   } 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:

  1. Open the URL http://localhost:4848/asadmin in a browser.
  2. Select the JavaMail Sessions node.
  3. Click New.
  4. Type mail/MySession in the JNDI Name field.
  5. Type the name of the host running your mail server in the Mail Host field.
  6. Type the destination email address in the Default User field.
  7. Type your email address in the Default Return Address field.
  8. Click OK.
  9. Note that mail/MySession is listed under the JavaMail Sessions node.

Deploying the Application

  1. In deploytool, open the ConfirmerApp.ear file, which resides in this directory:
  2. <INSTALL>/j2eetutorial14/examples/ejb/provided-ears/

  3. Verify the resource reference.
    1. In the tree, expand the ConfirmerApp node.
    2. Select the ConfirmerBean node.
    3. Select the Resource Ref's tab.
    4. Note the JavaMail resource reference for mail/TheMailSession.
  4. Verify the mapping of the reference to the JNDI name.
    1. In the tree, select the ConfirmerApp node.
    2. Click the Sun-specific Settings button.
    3. Note the mapping of mail/TheMailSession (coded in to mail/MySession.
  5. Deploy the ConfirmerApp application.
  6. In the Deploy Module dialog box, do the following:
    1. Select the Return Client JAR checkbox.
    2. In the field below the check box, enter the following:
    3. <INSTALL>/j2eetutorial14/examples/ejb/confirmer

Running the Client

To run the SavingsAccountClient program, do the following:

  1. In a terminal window, go to this directory:
  2. <INSTALL>/j2eetutorial14/examples/ejb/confirmer/

  3. Type the following command on a single line:
  4. appclient -client ConfirmerAppClient.jar your_email_address

  5. The client should display the following lines:
  6. ...
    Sending email to...

To modify this example, see the instructions in Modifying the J2EE Application.