users@glassfish.java.net

Re: Problem on configuring Java Mail Session

From: <glassfish_at_javadesktop.org>
Date: Thu, 13 Mar 2008 09:10:47 PST

*** It is a bug. ***

Sun inconsistently returns Session when you're inside their Glassfish EE container, and a MailConfiguration if you're inside a non-compliant portion of their Glassfish EE container or a 3rd party container. Here's a work around:
                
            // Outside the EJB container, MailSession lookups return a MailConfiguration rather
            // than a mail Session like one would expect.
            
            context = new InitialContext();
            Object object = context.lookup("mail/MyMailSession");
            Session session = null;
            
            if (object instanceof MailConfiguration)
            {
                    MailConfiguration configuration = (MailConfiguration) object;
                    session = Session.getInstance(configuration.getMailProperties());
            }
            else if (object instanceof Session)
            {
                    session = (Session) object;
            }

            // Compose the e-mail message.
            
            Message message = new MimeMessage(session);
[Message sent by forum member 'frenchdrip' (frenchdrip)]

http://forums.java.net/jive/thread.jspa?messageID=263855