users@glassfish.java.net

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

From: <glassfish_at_javadesktop.org>
Date: Fri, 08 Oct 2010 16:37:36 PDT

Actually I am using a simple Struts2 action class which I have annotated with @ManagedBean:

[b]@ManagedBean
public class ContactAction extends ActionSupport {

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

I know that this kind of design (a Struts2 Action class which is also a ManagedBean) is not a really good design. But I do not see any reason why the injection should not work.

Could the be any side effects because I use Struts2 - I hope not!


I have also tried it with the following class (complete soruce - you could copy and paste it):

[b]package com.xxx.xxx;
import javax.annotation.ManagedBean;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

//_at_Named
@ManagedBean
public class EmailBean {

    @Resource(name = "mail/contact")
    private Session mailSession;

    public boolean sendMessage() {
        System.out.println("START sendMessage()");
        System.out.println("mailSession==null:"+(mailSession==null));

        if (mailSession == null){
            System.out.println("JavaMail Resource Injection failed!");
        }

        Message msg = new MimeMessage(mailSession);

        try {

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

            //Exception comes up here:
            //javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
            Transport.send(msg);

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

        }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()");
        return false;
    }

}[/b]

I this source code I left out the JNDI lookup to make the source better to read. But here, of course, JNDI is also working just fine .

And guess wha: it is also not working.
See any problems here?

So I do not believe this here is a side effect related to Struts2...

ps
as you I have tried both @Named and @ManagedBean.
in my desperation i have also tried this in EmailBean:

@Inject @Named("mail/contact")
private Session mailSession;
[Message sent by forum member 'nabizamani']

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