java.lang.ClassCastException after the JNDI lookup of javax.mail.session
This issue is due to a mismatch of jar files. The jboss lib folder $JBOSS_HOME$/server/default/lib/ contains the mail.jar file. (Java Mail library) And as usualĀ you may have included the Java Mail Library jar in your deployed project. (.war or .ear). Both the libraries has the javax.mail.Session class. So the class loader will load the both as two different classes. This originates the issue.
javax.mail.Session session = (javax.mail.Session) context.lookup(“)
Here you going to load the java mail session from a jndi lookup. So the contex.lookup(“..”) returns a javax.mail.Session_JBOSS object. Now you try to cast it. But unfortunately the javax.mail.Session class used in your code is loaded as a different class (javax.mail.Session_USER). So java throws an ClassCastException.
Remove the java mail jar file from the lib folder of your application. It will work fine.
I have experienced the above issue for several other libraries too (log4j).
Hope this will help.
Rgs,
Lasith.