JavaMail Create Session Problem

Discussion in 'Java' started by Mikey, Jan 28, 2006.

  1. Mikey

    Mikey Guest

    I am attempting to use JavaMail to send an email from a servlet running
    under Tomcat. The code compiles fine but the servlet processing stops
    on an attempt to create a session instance. I'm new to Java and am
    probably making a basic mistake. The code segments are shown below. The
    processing stops at the "Session.." line with the Tomcat root cause
    error message:

    "java.lang.NoClassDefFoundError: javax/mail/Session"

    I've tried several variations of examples that I've found in Java
    references - with no luck.

    Any help would be appreciated.

    Thanks
    -------------------------------------------
    Code snippets:

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.Properties;
    ........
    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.host", "host mail server");
    props.setProperty("mail.user", "sender email");
    props.setProperty("mail.password", "my password");
    Session mailSession = Session.getDefaultInstance(props, null);
    try {
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setContent("This is a test", "text/plain");
    message.addRecipient(Message.RecipientType.TO,
    new InternetAddress(""));

    transport.connect();
    transport.sendMessage(message,
    message.getRecipients(Message.RecipientType.TO));
    transport.close();
    } catch (MessagingException ex) {
    while ((ex = (MessagingException)ex.getNextException()) != null) {
    ex.printStackTrace();
    }
    }
     
    Mikey, Jan 28, 2006
    #1
    1. Advertising

  2. Mikey

    James Westby Guest

    Mikey wrote:
    > I am attempting to use JavaMail to send an email from a servlet running
    > under Tomcat. The code compiles fine but the servlet processing stops
    > on an attempt to create a session instance. I'm new to Java and am
    > probably making a basic mistake. The code segments are shown below. The
    > processing stops at the "Session.." line with the Tomcat root cause
    > error message:
    >
    > "java.lang.NoClassDefFoundError: javax/mail/Session"
    >
    > I've tried several variations of examples that I've found in Java
    > references - with no luck.
    >
    > Any help would be appreciated.

    [snip]

    Do you have the JavaMail classes available to the servlet when running.
    i.e. do you have the JavaMail jar on the classpath somewhere?

    James
     
    James Westby, Jan 28, 2006
    #2
    1. Advertising

  3. Mikey

    Mikey Guest

    James Westby wrote:
    > Mikey wrote:
    > > I am attempting to use JavaMail to send an email from a servlet running
    > > under Tomcat. The code compiles fine but the servlet processing stops
    > > on an attempt to create a session instance. I'm new to Java and am
    > > probably making a basic mistake. The code segments are shown below. The
    > > processing stops at the "Session.." line with the Tomcat root cause
    > > error message:
    > >
    > > "java.lang.NoClassDefFoundError: javax/mail/Session"
    > >
    > > I've tried several variations of examples that I've found in Java
    > > references - with no luck.
    > >
    > > Any help would be appreciated.

    > [snip]
    >
    > Do you have the JavaMail classes available to the servlet when running.
    > i.e. do you have the JavaMail jar on the classpath somewhere?
    >
    > James
     
    Mikey, Jan 29, 2006
    #3
  4. Mikey

    Mikey Guest

    James,

    Thanks for your reply. I have the following in my classpath

    /opt/jakarta-tomcat-4.1.31/webapps/pdf/WEB-INF/lib/javamail-1.3.3_01/mail.jar:/opt/jakarta-tomcat-4.1.31/webapps/pdf
    /WEB-INF/lib/jaf-1.0.2/activation.jar

    These were the two jar files described in the JavaMail installation
    instructions.

    Mike
     
    Mikey, Jan 29, 2006
    #4
  5. Mikey wrote:
    > James,
    >
    > Thanks for your reply. I have the following in my classpath
    >
    > /opt/jakarta-tomcat-4.1.31/webapps/pdf/WEB-INF/lib/javamail-1.3.3_01/mail.jar:/opt/jakarta-tomcat-4.1.31/webapps/pdf
    > /WEB-INF/lib/jaf-1.0.2/activation.jar
    >
    > These were the two jar files described in the JavaMail installation
    > instructions.
    >


    Classloading inside of web containers works differently from
    classloading in other programs. The we container will usually supply an
    isolated classloading environment for each web application.

    My recommendation is for you to remove the above entries from the
    classpath for the Tomcat instance. Then, place the jars directly in the
    WEB-INF/lib directory for the web application (not in subdirectories as
    above).

    Let us know how it works out.

    HTH,
    Ray

    --
    This signature intentionally left blank.
     
    Raymond DeCampo, Jan 29, 2006
    #5
  6. Mikey

    James Westby Guest

    Mikey wrote:
    > James,
    >
    > Thanks for your reply. I have the following in my classpath
    >
    > /opt/jakarta-tomcat-4.1.31/webapps/pdf/WEB-INF/lib/javamail-1.3.3_01/mail.jar:/opt/jakarta-tomcat-4.1.31/webapps/pdf
    > /WEB-INF/lib/jaf-1.0.2/activation.jar
    >
    > These were the two jar files described in the JavaMail installation
    > instructions.
    >
    > Mike
    >


    Hi,

    So you have the Jars. That's a good start. It's a good idea to check
    that you were at least trying to make them accessible to the servlet.

    First you may wish to have a look, here.

    http://mindprod.com/Jgloss/runerrormessages.html#NOCLASSDEFFOUNDERROR

    It's a long list of the possible causes off this error, scan it and see
    if you think any of them might be affecting you.

    I have to apologise now as when I said are they on the classpath, I was
    being a little naive, as I'm not sure how Tomcat treats classpaths. You
    might want to read this, it was certainly informative for me,

    http://tomcat.apache.org/tomcat-4.1-doc/class-loader-howto.html


    It looks like you're trying to use the last classloader listed, WebAppX,
    which suggests that the jars go under webapp/WEB-INF/lib/. I'm flaky on
    classloaders, and so I'm not sure if they'll lok in subdirectories of
    that. I would recommend trying to move the jars directly in to lib/
    directory, and see if it works then. Worth a try I think. If it doesn't
    work then someone else with a bit more knowledge of Tomcat and
    classloaders might be able to help.

    Are you using any other external jars in your project?

    Have you verified that the jar actually contains the relevant class file
    (a long shot).


    James
     
    James Westby, Jan 29, 2006
    #6
  7. Mikey

    Mikey Guest

    Well, I tried a variety of combinations of places to put mail.jar and
    activation.jar. The combination that partially worked was to put the
    jar files in,

    /opt/jakarta-tomcat-4.1.31/common/lib

    and add a path to these jar files in CLASSPATH. However, the compile
    couldn't find,

    import javax.mail.*;
    import javax.mail.internet.*;

    unless CLASSPATH also included the path to the directories where I
    originally uploaded and installed JavaMail and the Activation software
    - which I added to CLASSPATH,


    /opt/jakarta-tomcat-4.1.31/webapps/pdf/WEB-INF/lib/javamail-1.3.3_01/mail.jar

    /opt/jakarta-tomcat-4.1.31/webapps/pdf/WEB-INF/lib/jaf-1.0.2/activation.jar

    Now, at least I'm able to create a session.

    I took this project over from someone who set up the Tomcat environment
    and I don't know why his jar file is where it is or why the CLASSPATH
    is configured the way it is. I'm partly just mimicking what he had
    done. Hence my confusion.

    I'm now getting an error at the next step of setting up the email for
    transmitting:

    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1
    <>... Relaying denied. IP name lookup failed
    [64.34.194.52]

    at
    com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:992)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:536)
    .....

    This one I'm going to tackle on my own. There's a lot to learn here.

    Thanks to all for your assistance.

    Mike
     
    Mikey, Jan 30, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news
    Replies:
    0
    Views:
    339
  2. Jason

    q javamail compile problem

    Jason, Dec 15, 2003, in forum: Java
    Replies:
    3
    Views:
    399
    Jason
    Dec 19, 2003
  3. Nilambari

    problem sending javamail

    Nilambari, Dec 23, 2003, in forum: Java
    Replies:
    2
    Views:
    8,331
    Ulas Ergin
    Dec 26, 2003
  4. mrby

    Problem about JavaMail

    mrby, Apr 29, 2004, in forum: Java
    Replies:
    1
    Views:
    469
    GaryM
    Apr 29, 2004
  5. Replies:
    4
    Views:
    1,839
    Roedy Green
    Jan 22, 2006
Loading...

Share This Page