tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Martin Gainty" <>
Subject Re: get a strange cast error with using the tomcat mail ressource
Date Mon, 12 Feb 2007 00:24:41 GMT
this is what I currently use in web.xml


import java.util.*;
import javax.mail.*;
import javax.naming.*;

     * Method used to login to the mail host.
    public void login() throws Exception {
        url = new URLName(protocol, getHostname(), -1, mbox, 
                          getUsername(), getPassword());
  * First, try to get the session from JNDI,
  * as would be done under J2EE.
 try {
     InitialContext ic = new InitialContext();
     Context ctx = (Context)ic.lookup("java:comp/env");
     session = (Session)ctx.lookup("MySession");
 } catch (Exception ex) {
     // ignore it

 // if JNDI fails, try the old way that should work everywhere
 if (session == null) {
     Properties props = null;
     try {
  props = System.getProperties();
     } catch (SecurityException sex) {
  props = new Properties();
     session = Session.getInstance(props, null);
        store = session.getStore(url);
        folder = store.getFolder(url);

(you will notice I didnt use lookup on 

This e-mail message (including attachments, if any) is intended for the use of the individual
or entity to which it is addressed and may contain information that is privileged, proprietary
, confidential and exempt from disclosure. If you are not the intended recipient, you are
notified that any dissemination, distribution or copying of this communication is strictly
Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant)
s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé
ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il
est strictement interdit de le diffuser, de le distribuer ou de le reproduire.
----- Original Message ----- 
From: "albrecht andrzejewski" <>
To: <>
Sent: Sunday, February 11, 2007 5:24 PM
Subject: get a strange cast error with using the tomcat mail ressource

Hello all,

I hope i will not pollute the list, but i'm affraid of being faced  
with a bug :-(

I have to send a mail with tomcat.
I followed the instructions of the tomcat documentation, corresponding  
my version (5.5).

Using this code:

  public static String sendMail(String destinataire, String login){

         Session session = null;
         try {
             Context initCtx = new InitialContext();
             Context envCtx = (Context) initCtx.lookup("java:comp/env");
line 36     session = (Session) envCtx.lookup("mail/MonMail");
             //session = envCtx.lookup("mail/MonMail");
         } catch (NamingException ex) {
               System.out.println("erreur au lookup");
            return ex.getMessage();

I have the following error that i really dislike :

java.lang.ClassCastException: javax.mail.Session cannot be cast to  
         at utils.MailHelper.sendMail(

I use the netbeans 5.5 bundled tomcat server (v 5.5.17) on the 1.6  
java version (but source level set to 1.5).

Do you think it's me ? Or a bug of the IDE ? or a bug of tomcat ?

I previously used this code with the same config in a successful way  
with another tomcat 5.5 server.

I'm getting crazy with this awful and strange cast error :-(
If only someone could help...

I join the web.xml tag:

             Resource reference to a factory for javax.mail.Session
             instances that may be used for sending electronic mail
             messages, preconfigured to connect to the appropriate
             SMTP server.

and my server.xml


and my context.xml

  <Resource name="mail/MonMail" auth="Container"

Hope to hear soon from the list about this problem :-(
Best regards.

Ce message a ete envoye par le serveur IMP de l'EMA.

To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message