The J2EETM Tutorial
Home
TOC
Index
PREV TOP NEXT Search
Feedback

Mail Session Connections

If you've ever ordered a product from a Web site, you've probably received an e-mail confirming your order. The ConfirmerBean class demonstrates how to send e-mail from an enterprise bean.

The source code for this example is in the j2eetutorial/examples/src/ejb/confirmer directory. To compile the code, go to the j2eetutorial/examples directory and type ant confirmer. A sample ConfirmerApp.ear file is in the j2eetutorial/examples/ears directory.

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. As with any resource, you must link the coded name (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(	
         "java:comp/env/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 ConfirmerEJB Example

Deploying the Application

  1. In deploytool, open the j2eetutorial/examples/ears/ConfirmerApp.ear file (FileOpen).
  2. In the Resource Refs tab of the bean, specify the resource reference for the mail session with the values shown in Table 16-1.

    Table 16-1 Resource Refs for the ConfirmerEJB Example
    Field Name
    Value
    Coded Name
    TheMailSession
    Type
    javax.mail.Session
    Authentication
    Application
    From
    (your email address)
    Host
    (mail server host)
    User Name
    (your UNIX or Windows user name)
  3. Deploy the SavingsAccountApp application (ToolsDeploy). In the Introduction dialog box, make sure that you select the Return Client JAR checkbox.

Running the Client

  1. In a terminal window, go to the j2eetutorial/examples/ears directory.
  2. Set the APPCPATH environment variable to ConfirmerAppClient.jar.
  3. Type the following command on a single line, replacing <recipient> with the e-mail address of the person who will receive the message.
       runclient -client ConfirmerApp.ear -name ConfirmerClient	
       -textauth <recipient>
     
    
  4. At the login prompts, enter guest for the user name and guest123 for the password.

Troubleshooting

If the application cannot connect to the mail server it will generate this exception:

javax.mail.MessagingException: Could not connect to SMTP host
 

To fix this problem, make sure that the mail server is running and that you've entered the correct name for the mail server host in the Resource Refs tab of the deploytool.

Home
TOC
Index
PREV TOP NEXT Search
Feedback