Write JavaMail applications

After you have configured e-mail services on your iSeries server and created a JavaMail Session object with the WAS administrative console, you can write applications that interface with these services through the Session object. According to the J2EE specification, each instance of the javax.mail.Session class is treated as a JNDI resource factory. To use the JavaMail APIs in an enterprise application component, such as a servlet, enterprise bean, or application client, perform the following steps:

  1. Use the WAS Application Assembly Tool (AAT) to declare mail resource references in your application.

    1. Open your application in the AAT. For more information on the AAT, see Application Assembly Tool.
    2. For an application, right-click Resource References in the navigation frame, and select New. For an enterprise application, expand Web Modules --> yourModule. Right-click Resource References, and select New.
    3. Fill in the following fields:

      • Name
        This is a required field. The Name field specifies the JNDI name of the resource relative to the java:comp/env context.
      • Description
        This is an optional field. The Description field specifies a description for the resource.
      • Type
        This is a required field. The Type field specifies the type of the resource. Select javax.mail.Session from the drop-down list.
      • Authentication
        The type of authentication to use for the resource. Select CONTAINER from the drop-down list.

    4. Save the application.

  2. The tool generates a deployment descriptor that has XML tags similar to those in the following example:

    <resource-ref>
       <description>description</description>
       <res-ref-name>mail/MailSession</res-ref-name>
       <res-type>javax.mail.Session</res-type>
       <res-auth>Container</res-auth>
    </resource-ref>
    
  3. Configure each JavaMail session object used by your Web component. This is done during the process of deploying the component.

  4. Use a JNDI lookup to get a reference to a Session object.

    See Example: Look up a JavaMail session for more information.

Example: JavaMail code

The following code illustrates how an application component sends a message and saves it to the mail account's Sent folder:

 
... 
// obtain JNDI initial context
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
// use JNDI lookup to obtain Session
mail_session = (javax.mail.Session) ctx.lookup
   ("java:comp/env/mail/MailSession");

// create new mail message object using Session
MimeMessage msg = new MimeMessage(mail_session);

// set message properties
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse
   ("somebody@some_domain.net"));
msg.setFrom(new InternetAddress("me@my_company.com"));
msg.setSubject("Important message from me");
String msg_text = new String("Hello!");
msg.setText(msg_text);

// get Session object's store
Store store = mail_session.getStore();
// connect to store
store.connect();
// obtain reference to "Sent" folder
Folder f = store.getFolder("Sent");
// create "Sent" folder if it does not exist
if (!f.exists()) f.create(Folder.HOLDS_MESSAGES);
// add message to "Sent" folder
f.appendMessages(new Message[] {msg});

Note: The preceding example uses the IMAP protocol for receiving e-mail messages. On the iSeries platform, it is supported by the Domino e-mail server, but not the TCP/IP Connectivity Utilities e-mail services, which only provides access to received messages through the POP3 protocol.