Skip to main content

Problem on configuring Java Mail Session

24 replies [Last post]
dabaner
Offline
Joined: 2008-03-10

Dear All,

I was trying to configure a Java mail session on Glassfish V2 using the admin console, everything looks fine. But when I tried to deploy my app I got the following exceptions. I am using Spring 2.5 to instantiate the mail session, it works on other environment e.g. websphere and weblogic etc. but it encountered type mismatch on glassfish. I have read some articles suggesting to use MailConfiguration instead of MailSession in application, but that should be just a workaround. How can I solve this in a more appropriate way? What are matching editor or conversion strategy meant for?

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailSender' defined in ServletContext resource : Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [com.sun.enterprise.deployment.MailConfiguration] to required type [javax.mail.Session] for property 'session'; nested exception is java.lang.IllegalArgumentException: No matching editors or conversion strategy found
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailSender' defined in ServletContext resource: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [com.sun.enterprise.deployment.MailConfiguration] to required type [javax.mail.Session] for property 'session'; nested exception is java.lang.IllegalArgumentException: No matching editors or conversion strategy found
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [com.sun.enterprise.deployment.MailConfiguration] to required type [javax.mail.Session] for property 'session'; nested exception is java.lang.IllegalArgumentException: No matching editors or conversion strategy found
Caused by: java.lang.IllegalArgumentException: No matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:212)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:127)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:774)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:607)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:49)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:74)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:140)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:248)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:950)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:140)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:248)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:950)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBeanDefinition(BeanDefinitionValueResolver.java:200)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:111)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:950)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:140)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:248)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:950)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBeanDefinition(BeanDefinitionValueResolver.java:200)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:111)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:950)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:140)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:248)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:950)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBeanDefinition(BeanDefinitionValueResolver.java:200)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:111)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:950)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:140)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:248)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:950)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBeanDefinition(BeanDefinitionValueResolver.java:200)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:111)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:950)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:740)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:417)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:140)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:270)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:346)
at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:156)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4523)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5184)
at com.sun.enterprise.web.WebModule.start(WebModule.java:326)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:973)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:957)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:688)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1584)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1222)
at com.sun.enterprise.server.WebModuleDeployEventListener.moduleDeployed(WebModuleDeployEventListener.java:182)
at com.sun.enterprise.server.WebModuleDeployEventListener.moduleDeployed(WebModuleDeployEventListener.java:278)
at com.sun.enterprise.admin.event.AdminEventMulticaster.invokeModuleDeployEventListener(AdminEventMulticaster.java:974)
at com.sun.enterprise.admin.event.AdminEventMulticaster.handleModuleDeployEvent(AdminEventMulticaster.java:961)
at com.sun.enterprise.admin.event.AdminEventMulticaster.processEvent(AdminEventMulticaster.java:464)
at com.sun.enterprise.admin.event.AdminEventMulticaster.multicastEvent(AdminEventMulticaster.java:176)
at com.sun.enterprise.admin.server.core.DeploymentNotificationHelper.multicastEvent(DeploymentNotificationHelper.java:308)
at com.sun.enterprise.deployment.phasing.DeploymentServiceUtils.multicastEvent(DeploymentServiceUtils.java:226)
at com.sun.enterprise.deployment.phasing.ServerDeploymentTarget.sendStartEvent(ServerDeploymentTarget.java:298)
at com.sun.enterprise.deployment.phasing.ApplicationStartPhase.runPhase(ApplicationStartPhase.java:132)
at com.sun.enterprise.deployment.phasing.DeploymentPhase.executePhase(DeploymentPhase.java:108)
at com.sun.enterprise.deployment.phasing.PEDeploymentService.executePhases(PEDeploymentService.java:919)
at com.sun.enterprise.deployment.phasing.PEDeploymentService.start(PEDeploymentService.java:591)
at com.sun.enterprise.deployment.phasing.PEDeploymentService.start(PEDeploymentService.java:635)
at com.sun.enterprise.admin.mbeans.ApplicationsConfigMBean.start(ApplicationsConfigMBean.java:744)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.enterprise.admin.MBeanHelper.invokeOperationInBean(MBeanHelper.java:375)
at com.sun.enterprise.admin.MBeanHelper.invokeOperationInBean(MBeanHelper.java:358)
at com.sun.enterprise.admin.config.BaseConfigMBean.invoke(BaseConfigMBean.java:464)
at com.sun.jmx.mbeanserver.DynamicMetaDataImpl.invoke(DynamicMetaDataImpl.java:213)
at com.sun.jmx.mbeanserver.MetaDataImpl.invoke(MetaDataImpl.java:220)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:815)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:784)
at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.enterprise.admin.util.proxy.ProxyClass.invoke(ProxyClass.java:90)
at $Proxy1.invoke(Unknown Source)
at com.sun.enterprise.admin.server.core.jmx.SunoneInterceptor.invoke(SunoneInterceptor.java:304)
at com.sun.enterprise.interceptor.DynamicInterceptor.invoke(DynamicInterceptor.java:174)
at com.sun.enterprise.deployment.client.DeploymentClientUtils.startApplication(DeploymentClientUtils.java:145)
at com.sun.enterprise.deployment.client.DeployAction.run(DeployAction.java:537)
at java.lang.Thread.run(Thread.java:595)

Message was edited by: dabaner

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ryen
Offline
Joined: 2004-03-05

When I search "*mail.jar" under glassfish v3 tp2 and Prelude, there is no such file. However, glassfish v2 has mail.jar under lib. Are you sure the GF V3 has JavaMail module or implementation in it? I've also brought up updatetool to check the installed modules, but didn't see mail module.

I've modified Liferay code, instead of lookup mailSession JNDI registery, I put all those mailSession properties in a property obj, and init a MailSession obj with it. As you said, it's visible across whole web container, but Liferay still cannot send out email. Could you verify that GFV3 has mail implementation in it?

Thank you,
Robert

peterwx
Offline
Joined: 2005-06-24

In Prelude, java mail appears to be packaged as glassfish/modules/javax.mail-10.0-SNAPSHOT.jar

I didn't find it in TP2 (which is very old anyway, use the latest Prelude promoted builds instead -- http://download.java.net/glassfish/v3/promoted/ ).

xshuang
Offline
Joined: 2008-04-14

Just add type=javax.mail.Session.class, then you don't need to consider whether the returned type is MailConfiguration or Session.

Sahoo

The correct way to look up resource in Java EE is to use the local name
(a.k.a. logical name) of the resource rather than the actual JNDI name
of the resource. You can run the following sample and see the difference:

@Stateless()
@WebService()
public class VisitorRegistrationService {

// The actual JNDI name is mail/FossDemoMailSession
// where as the local name is mail/MailSession.
@Resource(name="mail/MailSession",
mappedName="mail/FossDemoMailSession")
Session mailSession; // inject a Mail Session
/**
* Web service operation
*/
@WebMethod
public int registerVisitor(
String name,
String email) {
try {
InitialContext ctx = new InitialContext();
Object a = ctx.lookup("mail/FossDemoMailSession");
Object b = ctx.lookup("java:comp/env/mail/MailSession");
System.out.println(a);
System.out.println(b);
} catch(Exception e) {
e.printStackTrace();
}
return 0;
}
}

Looking up the resource using the actual JNDI name
(mail/FossDemoMailSession) returns
com.sun.enterprise.deployment.MailConfiguration@49d560 object, where as
if you use java:comp/env/mail/MailSession, it returns
javax.mail.Session@157bbd4.

To try out yourself, just compile, jar and deploy. Create a MailSession
using admin gui and give it a name mail/FossDemoMailSession. You can
test the program by using the web service test client that is available
in the admin gui.

Thanks,
Sahoo

glassfish@javadesktop.org wrote:
> Hi Sahoo,
> Below post from 'Frenchdrip' pointed out what my doubt was. Actually I tried followig different ways,
> 1. directly using resource injection on setter or instance variable
> 2. inject resource on class level and lookup it from sessionContext
> 3. inject resource on class level and lookup it from JNDI initial context.
>
> Only 1 gets mailSession object. Both 2 and 3 return mail configuration which requires casting to session. I had never experienced such as well known 'NameNotFoundException' so I don't believe that's a JNDI problem.
>
> Thanks,
> Ken
> [Message sent by forum member 'dabaner' (dabaner)]
>
> http://forums.java.net/jive/thread.jspa?messageID=264257
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

dabaner
Offline
Joined: 2008-03-10

You sample code works because the @Resource(name="mail/MailSession", mappedName="...") has been injected to the Session object, as such it is "recognized" as MailSession in this context...

Could you please try to move @Resource(name="mail/MailSession", mappedName=...) up to the class level, and then look it inside the class,
e.g.
@Resource(name="mail/MailSession", mappedName="FossDemoMailSession")
@Stateless

public class VisitorRegistrationService{
....
void someMethod{
InitialContext ctx = new InitialContext();
Object a = ctx.lookup("mail/FossDemoMailSession");
Object b = ctx.lookup("java:comp/env/mail/MailSession");
...
You will get that ClassCastException!

Please note that, I changed the SessionEJB from WebService, because I was experiencing problem when I annotated @Resource on the class level, the MailSession could not be injected even it is legal according to the EJB 3 Spec...

Sahoo

While using @Resource at the class level, use "type" field of @Resource
to indicate the type of the resource. The javadoc of @Resource says the
following:

/**
* The Java type of the resource. For field annotations,
* the default is the type of the field. For method annotations,
* the default is the type of the JavaBeans property.
* For class annotations, there is no default and this must be
* specified.
*/
Class type() default java.lang.Object.class;

So, you should do something like this:

@Resource(name="mail/MailSession3", type=Session.class,
mappedName="mail/FossDemoMailSession")

Without the type attribute, @Resource is ignored. If you run verifier
against such an app, it will report a nice error.

Thanks,
Sahoo

glassfish@javadesktop.org wrote:
> You sample code works because the @Resource(name="mail/MailSession", mappedName="...") has been injected to the Session object, as such it is "recognized" as MailSession in this context...
>
> Could you please try to move @Resource(name="mail/MailSession", mappedName=...) up to the class level, and then look it inside the class,
> e.g.
> @Resource(name="mail/MailSession", mappedName="FossDemoMailSession")
> @Stateless
>
> public class VisitorRegistrationService{
> ....
> void someMethod{
> InitialContext ctx = new InitialContext();
> Object a = ctx.lookup("mail/FossDemoMailSession");
> Object b = ctx.lookup("java:comp/env/mail/MailSession");
> ...
> You will get that ClassCastException!
>
> Please note that, I changed the SessionEJB from WebService, because I was experiencing problem when I annotated @Resource on the class level, the MailSession could not be injected even it is legal according to the EJB 3 Spec...
> [Message sent by forum member 'dabaner' (dabaner)]
>
> http://forums.java.net/jive/thread.jspa?messageID=264975
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

dabaner
Offline
Joined: 2008-03-10

Hi Sahoo,

Thanks for that. The @Resource at class level works either.

Can I ask you a further question on this, that is how to work it out using deployment descriptor instead? Below are ejb-jar.xml and sun-ejb-jar.xml I am using, when I added them into your example and commented out the @Resource annotation, I got both a and b are MailConfiguration, not the desired MailSession.
Can you please have a look and let me know if I did something wrong?
ejb-jar.xml

version = "3.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">


VisitorRegistrationService
ejb.VisitorRegistrationService

mail/MailSession
javax.mail.Session




sun-ejb-jar.xml





VisitorRegistrationService
VisitorRegistrationService

mail/MailSession
mail/FossDemoMailSession



Thanks
Ken

Sahoo

Use resource-ref instead of resource-env-ref. Following is an example of
what you need to inject a mail.Session into your EJB without using
annotation.


mail/MailSession
javax.mail.Session
mail/FossDemoMailSession

ejb.VisitorRegistrationService
mailSession

It uses mappedName entry, so no need for using sun-ejb-jar.xml for
mapping the local name to global JNDI name.

There is a trick that you can use in future to find out the exact DD
required for such cases:
1. deploy the original sample which uses annotations.
2. Open to
$GF_HOME/domains/domain1/generated/xml/j2ee-module//META-INF/ejb-jar.xml.
You can now see the equivalent ejb-jar.xml that GlassFish has generated
from your annotations in source code. Copy and paste relevant sections
into your ejb-jar.xml.

Thanks,
Sahoo

glassfish@javadesktop.org wrote:
> Hi Sahoo,
>
> Thanks for that. The @Resource at class level works either.
>
> Can I ask you a further question on this, that is how to work it out using deployment descriptor instead? Below are ejb-jar.xml and sun-ejb-jar.xml I am using, when I added them into your example and commented out the @Resource annotation, I got both a and b are MailConfiguration, not the desired MailSession.
> Can you please have a look and let me know if I did something wrong?
> ejb-jar.xml
>
>
> > version = "3.0"
> xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
>
>
> VisitorRegistrationService
> ejb.VisitorRegistrationService
>
> mail/MailSession
> javax.mail.Session
>
>

>

>

>

>
> sun-ejb-jar.xml
>
>
>
>
>
> VisitorRegistrationService
> VisitorRegistrationService
>
> mail/MailSession
> mail/FossDemoMailSession
>

>

>

>

>
>
> Thanks
> Ken
> [Message sent by forum member 'dabaner' (dabaner)]
>
> http://forums.java.net/jive/thread.jspa?messageID=265260
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

dabaner
Offline
Joined: 2008-03-10

Thanks a lot, Sahoo!

ryen
Offline
Joined: 2004-03-05

Hi All,

Trying to configure JavaMail in Glassfish V3 TP2, but according to few Sun email threads, i.e. http://markmail.org/message/dk7ty24u3aob3lqe, asadmin create-javamail-resource command was removed, and javamail resource menu entry was removed from admin GUI. Another email indicated there is no backend support for these commands yet. Has Glassfish V3 TP2 kept its JavaMail feature? If so, how could admin user configure it?

Any helps highly appreciated.

Robert

peterwx
Offline
Joined: 2005-06-24

GlassFish V3 (TP2 or the upcoming Prelude release) does not support mail resources yet.

The mail api is included, but you'll have to create and manage the sessions yourself if you want to use such code with V3 right now.

ryen
Offline
Joined: 2004-03-05

Could you provide me a sample, or point me to the resources where I could find one?

I'm currently deploying Liferay 5.1 to Glassfish v3 tp2. Has anyone implement similar JavaMail session management code in Websynergy?

Thank you for your help!

peterwx
Offline
Joined: 2005-06-24

I don't have one handy, but you could download javamail 1.4.1 and integrate one of the J2SE samples included with that distribution.

Construct the session and properties inline rather than relying on properties (or use system properties, but be aware they will be available across the entire application server, not recommended.)

I might have time later to write something up, but certainly not this week. Lots of work still to do on NetBeans 6.5 and GF V3.

ryen
Offline
Joined: 2004-03-05

Thank you very much. I'll take a look of javamail 1.4.1 with J2SE integration.

Please let me know if you could write the sample javamail for GF V3.

Robert

frenchdrip
Offline
Joined: 2007-11-05

*** 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);

uppalapati
Offline
Joined: 2006-05-05

Can you please file an issue on Glassfish V2 to get this issue fixed in the next update for Glassfish?

peterwx
Offline
Joined: 2005-06-24

No need. This is a known problem, issue #4500 ( https://glassfish.dev.java.net/issues/show_bug.cgi?id=4500 )

Note that while the behavior is undesirable (and I consider it a bug), it is not a spec violation.

Feel free to add your name(s) to the CC field of the above issue if you wish to track it's progress.

ryen
Offline
Joined: 2004-03-05

Could you provide me a sample, or point me to the resources where I could find one?

I'm currently deploying Liferay 5.1 to Glassfish v3 tp2. Has anyone implement similar JavaMail session management code in Websynergy?

Thank you for your help!

dabaner
Offline
Joined: 2008-03-10

The Java Mail session created on Glassfish works well if it is injected as @Resource (name="mail/myMail"), but it fails on direct using JNDI as following,
...
InitialContext ctx = new InitialContext();
Session session = (Session)ctx.lookup("mail/myMail");
...
the exceptioin is exactly the same as the spring case. MailConfiguration can not be cast to MailSession etc. Can someone give some lights on this?

Sahoo

If @Resource (name="mail/myMail") was successfully injected, then you
should look up the resource in "java:comp/env" namespace as shown below:

new InitialContext().lookup("java:comp/env/mail/myMail");

Thanks,
Sahoo
glassfish@javadesktop.org wrote:
> The Java Mail session created on Glassfish works well if it is injected as @Resource (name="mail/myMail"), but it fails on direct using JNDI as following,
> ...
> InitialContext ctx = new InitialContext();
> Session session = (Session)ctx.lookup("mail/myMail");
> ...
> the exceptioin is exactly the same as the spring case. MailConfiguration can not be cast to MailSession etc. Can someone give some lights on this?
> [Message sent by forum member 'dabaner' (dabaner)]
>
> http://forums.java.net/jive/thread.jspa?messageID=263316
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

dabaner
Offline
Joined: 2008-03-10

Thanks for you reply.
Using "java:comp/env" as prefix didn't work, the application would encounter name not found exception. Actually using lookup("mail/MyMail") had found the resource but it failed to instantiate the Session object due to something else e.g. ClassCasting exception as the resource was recognized as mail configuration etc.
I am assuming in the scenario of dependency injection Glassfish might have done something internally convert the "MailConfiguration" to mail Session, but more likely in the JNDI scenario the convertion Glassfish has left the conversion to application developer! I don't think this is desirable for both Glassfish and application developer, please correct me if I have missed something.
Please note that, I have also tried using following code, it didn't work either,

@Resource (name="mail/MyMail")
public class MyMailerBean {
@Resource SessionContext ctx;
public void sendMail(){
javax.mail.Session session = (javax.mail.Session)ctx.lookup("mail/MyMail");
......

Sahoo

Of course, you would NameNotFoundException if you just tried to look up
the resource without the injection code. Read chapter 5 of the Java
Platform Spec, where it is clearly described how you should be looking
up a resource and java:comp/env works.

Thanks,
Sahoo

glassfish@javadesktop.org wrote:
> Thanks for you reply.
> Using "java:comp/env" as prefix didn't work, the application would encounter name not found exception. Actually using lookup("mail/MyMail") had found the resource but it failed to instantiate the Session object due to something else e.g. ClassCasting exception as the resource was recognized as mail configuration etc.
> I am assuming in the scenario of dependency injection Glassfish might have done something internally convert the "MailConfiguration" to mail Session, but more likely in the JNDI scenario the convertion Glassfish has left the conversion to application developer! I don't think this is desirable for both Glassfish and application developer, please correct me if I have missed something.
> Please note that, I have also tried using following code, it didn't work either,
>
> @Resource (name="mail/MyMail")
> public class MyMailerBean {
> @Resource SessionContext ctx;
> public void sendMail(){
> javax.mail.Session session = (javax.mail.Session)ctx.lookup("mail/MyMail");
> ......
> [Message sent by forum member 'dabaner' (dabaner)]
>
> http://forums.java.net/jive/thread.jspa?messageID=263770
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

dabaner
Offline
Joined: 2008-03-10

Hi Sahoo,
Below post from 'Frenchdrip' pointed out what my doubt was. Actually I tried followig different ways,
1. directly using resource injection on setter or instance variable
2. inject resource on class level and lookup it from sessionContext
3. inject resource on class level and lookup it from JNDI initial context.

Only 1 gets mailSession object. Both 2 and 3 return mail configuration which requires casting to session. I had never experienced such as well known 'NameNotFoundException' so I don't believe that's a JNDI problem.

Thanks,
Ken

Sahoo

Give me sometime, I shall get back to you after writing a sample to test
it myself.

Thanks,
Sahoo

glassfish@javadesktop.org wrote:
> Hi Sahoo,
> Below post from 'Frenchdrip' pointed out what my doubt was. Actually I tried followig different ways,
> 1. directly using resource injection on setter or instance variable
> 2. inject resource on class level and lookup it from sessionContext
> 3. inject resource on class level and lookup it from JNDI initial context.
>
> Only 1 gets mailSession object. Both 2 and 3 return mail configuration which requires casting to session. I had never experienced such as well known 'NameNotFoundException' so I don't believe that's a JNDI problem.
>
> Thanks,
> Ken
> [Message sent by forum member 'dabaner' (dabaner)]
>
> http://forums.java.net/jive/thread.jspa?messageID=264257
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net