Sun Java System Application Server Enterprise Edition 8.1 2005Q2 Developer's Guide

- Previous: JavaMail Session Properties
- Next: Sending and Reading Messages Using JavaMail

# Looking Up a JavaMail Session

The standard Java Naming and Directory InterfaceTM (JNDI) subcontext for JavaMail sessions is `java:comp/env/mail`.

Registering JavaMail sessions in the `mail` naming subcontext of a JNDI namespace, or in one of its child subcontexts, is standard. The JNDI namespace is hierarchical, like a file system’s directory structure, so it is easy to find and nest references. A JavaMail session is bound to a logical JNDI name. The name identifies a subcontext, `mail`, of the root context, and a logical name. To change the JavaMail session, you can change its entry in the JNDI namespace without having to modify the application.

The resource lookup in the application code looks like this:

    InitialContext ic = new InitialContext();
    String snName = "java:comp/env/mail/MyMailSession";
    Session session = (Session)ic.lookup(snName);

For more information about the JNDI API, see Chapter 13, Using the Java Naming and Directory Interface.

- Previous: JavaMail Session Properties
- Next: Sending and Reading Messages Using JavaMail

- © 2010, Oracle Corporation and/or its affiliates