dev@glassfish.java.net

Javamail/JNDI help?

From: Gary Horton <Gary.Horton_at_Sun.COM>
Date: Wed, 20 Jun 2007 08:34:20 -0600

Ken Paulsen has pointed me here to ask for help wrt correctly setting up
a JavaMail session in glassfish. I have set up the session, but cannot
successfully locate the resource using JNDI.

    My goal is to send out a simple text message via javamail from my
webapp, deployed to glassfish. From reading the Admin manual
(http://docs.sun.com/app/docs/doc/819-3658/6n5s5nkm4?a=view), I
configure JavaMail sessions with the following JNDI names:

comp/env/mail/awardsNotification
mail/awardsNotification
awardsNotification

    I used several variations because my initial attempt using just the
1st one (comp/env/mail/awardsNotification) didn't work out, so I thought
I must have been approaching the naming incorrectly.

    I then put together some test code that in effect does what the
glassfish manual describes
(https://glassfish.dev.java.net/javaee5/docs/DG/beaow.html):

 private Session getMailSession(String jndiName) {
     try {
         InitialContext ic = new InitialContext();
         logger.info(jndiName + "...");
         Session session = (Session)ic.lookup(jndiName);
         logger.info("Success!");
         return session;
     } catch (Exception ex) {
         this.logger.error("Failed: " + ex);
         return null;
     }
 }

Then I try it out finding any of the 3 JNDI resources with each of the
following JNDI name arguments, but they all fail. Here are the arguments
sent to the above method:

   1. mail:comp/env/mail/awardsNotification
   2. java:comp/env/mail/awardsNotification
   3. java:mail/awardsNotification
   4. mail:mail/awardsNotification
   5. comp/env/mail/awardsNotification
   6. mail/awardsNotification
   7. mail:awardsNotification
   8. awardsNotification

The output generated from these tests tell me, when I use #5, #6 or #8,
that I'm getting back a MailConfiguration object instead of a Session
object. I believe this is the problem, but I have no clue how to remedy
this.

Here's the detailed output from invoking the getMailSession method with
the above arguments:

   1. mail:comp/env/mail/awardsNotification...Failed: null
   2. java:comp/env/mail/awardsNotification...Failed: No object bound to
      name java:comp/env/mail/awardsNotification
   3. java:mail/awardsNotification...Failed: No object bound to name
      java:mail/awardsNotification
   4. mail:mail/awardsNotification...Failed: null
   5. comp/env/mail/awardsNotification...Failed:
      java.lang.ClassCastException:
      com.sun.enterprise.deployment.MailConfiguration
   6. mail/awardsNotification...Failed: java.lang.ClassCastException:
   7. mail:awardsNotification...Failed: mail:awardsNotification not found
   8. awardsNotification...Failed: java.lang.ClassCastException:
      com.sun.enterprise.deployment.MailConfiguration

Can someone tell me what I'm doing wrong? Thanks in advance - and if you
would, please cc me in your reply, since I'm not on this alias --
-gh