Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 3 (10.1.3)
B14428-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

Configuring an Environment Reference to a Java Mail Resource Manager Connection Factory

You can access a Java mail session by creating a resource manager connection factory reference to it.


Note:

In EJB 3.0, an environment reference to a resource manager connection factory is not needed. You can access a resource manager connection factory directly using annotations and resource injection (see "Looking Up an EJB 3.0 Resource Manager Connection Factory").

For information on looking up a resource manager connection factory, see:

To define a reference to a Java mail resource manager connection factory:

  1. Bind the javax.mail.Session reference within the JNDI name space in the application.xml file using the <mail-session> element, as follows:

    <mail-session location="mail/MailSession"
       smtp-host="mysmtp.oraclecorp.com">
       <property name="mail.transport.protocol" value="smtp"/>
       <property name="mail.smtp.from" value="emailaddress@oracle.com"/>
    </mail-session>
    
    

    The location attribute contains the JNDI name specified in the location attribute of the <resource-ref-mapping> element in the OC4J-specific deployment descriptor.

  2. Create a logical name within the <res-ref-name> element in the EJB deployment descriptor.

    It is a best practice to start the reference name with "mail" but it is not required. In the bean code, the lookup of this reference is always prefaced by "java:comp/env" (for example, "java:comp/env/mail/myMail")

  3. Map the logical name within the EJB deployment descriptor to the JNDI name, created in step 1, within the OC4J-specific deployment descriptor.

  4. Lookup the object reference within the bean with the "java:comp/env/mail" preface and the logical name defined in the EJB deployment descriptor.

As shown in Figure 19-3, the Session object was bound to the JNDI name "/test/myMailSession". The logical name that the bean knows this resource as is "mail/testMailSession". These names are mapped together within the OC4J-specific deployment descriptor. Thus, within the bean's implementation, the bean can retrieve the connection to the bound Session object by using the "java:comp/env/mail/testMailSession" environment element.

Figure 19-3 Session Resource Manager Mapping

This figure shows the session resource manager mapping.

This environment element is defined with the following information:

Element Description
<res-ref-name> The logical name of the Session object to be used within the originating bean. The name should be prefixed with "mail/". In our example, the logical name for our mail session is "mail/testMailSession".
<res-type> The Java type of the resource. For the Java mail Session object, this is javax.mail.Session.
<res-auth> Define who is responsible for signing on to the database. The value can be "Application" or "Container" based on who provides the authentication information.

Example 19-11 Defining an environment element for Java mail Session

The environment element is defined within the EJB deployment descriptor by providing the logical name, "mail/testMailSession", its type of javax.mail.Session, and the authenticator of "Application".

Defining an environment element for Java mail session.

The environment element of "mail/testMailSession" is mapped to the JNDI bound name for the connection, "test/myMailSession" within the OC4J-specific deployment descriptor.

This figure shows the environment element mapping.

Once deployed, the bean can retrieve the Session object reference:

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

//The following uses the mail session object
//Create a message object
MimeMessage msg = new MimeMessage(session);

//Construct an address array
String mailTo = "whosit@oracle.com";
InternetAddress addr = new InternetAddress(mailto);
InternetAddress addrs[] = new InternetAddress[1];
addrs[0] = addr;

//set the message parameters
msg.setRecipients(Message.RecipientType.TO, addrs);
msg.setSubject("testSend()" + new Date());
msg.setContent(msgText, "text/plain");

//send the mail message
Transport.send(msg);