users@glassfish.java.net

GF 3.0.1 Bug: JavaMail Rsource Injection fails but JNDI lookup works!

From: <glassfish_at_javadesktop.org>
Date: Fri, 08 Oct 2010 07:33:52 PDT

Hi,

I am trying to setup a JavaMail resource in Glassfish. For setting this up I have simply used the following asadmin CLI command (found on http://weblogs.java.net/blog/felipegaucho/archive/2010/03/04/glassfish-v3-resources-administration-cli-tool-asadmin):

asadmin --interactive=false create-javamail-resource --mailhost=$MAIL_HOST --mailuser=$MAIL_USER --fromaddress=$MAIL_FROM --enabled=true --description="My Description" --storeprotocol=imap --storeprotocolclass=com.sun.mail.imap.IMAPStore --transprotocol smtp --transprotocolclass com.sun.mail.smtp.SMTPSSLTransport --property mail-smtp.user=$MAIL_SMTP_USER:mail-smtp.port=465:mail-smtp.password=$MAIL_SMTP_PASSWORD:mail-smtp.auth=true:mail-smtp.socketFactory.fallback=false:mail-smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory:mail-smtp.socketFactory.port=$MAIL_SMTP_PORT:mail-smtp.starttls.enable=true
mail/contact

This works just fine with the parameters I pass to this command.
But unfortunately Resource Injection does not work:

[...]
[b]@Resource(name = "mail/contact")
private Session mailSession;

public void sendMessage() {
        System.out.println("START sendMessage()");
        System.out.println("mailSession==null:"+(mailSession==null));
        
        if (mailSession == null){
            //always entered because mailSession injection failed
            System.out.println("JavaMail Resource Injection failed!");
            System.out.println("Use JNDI lookup for JavaMail instead...");
            try {
                InitialContext ic = new InitialContext();
                String snName = "java:comp/env/mail/contact";
                mailSession = (Session) ic.lookup(snName);
            } catch (NamingException ex) {
                //never entered
                System.out.println("JNDI lookup also failed");
            }
        }

        Message msg = new MimeMessage(mailSession);

        try {

            msg.setSubject("[app] Contact Request");
            msg.setRecipient(RecipientType.TO,new InternetAddress("xxx_at_xxx.com",
"XXX"));
            msg.setText("This is a Test Email");
            Transport.send(msg);

            System.out.println("Email sent SUCCESFULLY.");
            System.out.println("END sendMessage()");
            return;

        }catch(MessagingException me) {
            System.out.println("MessagingException:");
            me.printStackTrace();

        }catch(Throwable t) {
            System.out.println("Throwable:");
            t.printStackTrace();
        }
        System.out.println("Email could not be sent.");
        System.out.println("END sendMessage()");
    }
[/b][...]

As I have debugged mailSession is always null, that is why I get this exception:
[b]javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;[/b]

This means that JNDI lookup works fine, but Resource
Injection is not working. It also shows that the asadmin command (see above) I executed to create the JavaMail resource on Glassfish works just fine - else sending the email via JNDI would also have failed (but it is working with JNDI).

This issue seems to be similar to an old thread I found:
http://forums.java.net/jive/thread.jspa?messageID=265663

I have also entered a bug report:
https://glassfish.dev.java.net/issues/show_bug.cgi?id=13836

Using
[b]@Resource(mappedName = "mail/contact")[/b]
instead of
[b]@Resource(name = "mail/contact")[/b]

as suggested there is also not working.
As I believe [b]@Resource(name = "mail/contact")[/b] should definitely be working (see https://glassfish.dev.java.net/javaee5/ejb/compdependencies_xmlforum_nov15.pdf on slide 12).

So my conclusion is ver clear:
1. [b]@Resource(mappedName = "mail/contact")[/b] is not working
2. [b]@Resource(name = "mail/contact")[/b] is not working
3. JNDI lookup worsk just fine
4. asadmin CLI was correct (because of 3)

==> This is a Glassfish Bug!

Can anyone confirm this here?
My Goal is to have this fixed as soon as possible by the Glassfish guys. As a worksround I will simply stick with JNDI.

Regards,
Nabi
[Message sent by forum member 'nabizamani']

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