The J2EETM Tutorial
Home
TOC
PREV TOC NEXT 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 ConfirmerEJB class demonstrates how to send email from an enterprise bean.

The source code for this example is in the examples/src/ejb/confirmer directory. To compile the code, go to the examples/src directory and type ant confirmer.

In the sendNotice method of the ConfirmerEJB 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 in the Resource Ref's tab of the deploytool. (See Table 30.) 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 ConfirmerEJB");
  
       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());
   }
} 

Tips for Running the ConfirmerEJB Example

  1. Before compiling the ConfirmerClient.java program, change the value of the recipient variable to an actual email address.
  2. In the Resource Ref's tab of the bean, specify the resource reference for the mail session with the values in the following table.

    Table 29 Resource Ref's for the ConfirmerEJB Example
    Dialog Field
    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. Use the JNDI names listed in the following table.

    Table 30 JNDI Names for the ConfirmerEJB Example 
    Component or
    Reference Name
    JNDI Name
    ConfirmerBean
    MyConfirmer
    TheMailSession
    MyMailer

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 Ref's tab.

Home
TOC
PREV TOC NEXT Search
Feedback