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