Using JavaMail in Orion

Written by Hani Suleiman

n Orion, you can store a javax.mail.Session in JNDI at the container level. The reason one might wish to do this is the same reason you might want to do so for JDBC: to put server-specific SMTP connection information into the deployment mechanism rather than into the code being deployed. In simpler terms, you can have your development server connect to, say, dev.smtp.yourcompany.com - and your production server can connect to your production mail system.

Doing so is fairly simple, although there are a number of steps involved. The first is the creation of the mail session in your application, via the orion-application.xml file. This is as simple as putting the following line in the corret place (generally following the principals node):

<mail-session location="mail/MailSession" smtp-host="smtp.yourhost.com" />
 

This creates an entry in the global JNDI tree, at "mail/MailSession." The next step is to get your application (at the web-application, ejb, or enterprise application layer) to have a local JNDI name to look into for the session. For simplicity's sake, we'll use the web application here, although no other module would do it significantly differently.

In your web.xml, add the following resource reference, which creates a JNDI reference at "java:comp/env/javamail/session" that is of type javax.mail.Session:

<resource-ref>
        <res-ref-name>javamail/session</res-ref-name>
        <res-type>javax.mail.Session</res-type>
        <res-auth>CONTAINER</res-auth>
</resource-ref>

The next step, now that we've declared the local reference, is to resolve the local reference and point it to the global reference. For a web application, this is done in orion-web.xml, which should have a node as follows:

<resource-ref-mapping location="mail/MailSession" name="javamail/session" />

The final step is, of course, to use the Session. This uses standard JNDI semantics, so example code might be the following:

Context ctx=new InitialContext(); 
javax.mail.Session mailSession = 
           (javax.mail.Session)ctx.lookup("java:comp/env/javamail/session"); 
// use the session as normal 

Copyright © 2007 IronFlare AB