Solved

Sending Email with Javamail doesn't work with J2EE 1.4 Final

Posted on 2003-12-10
4
988 Views
Last Modified: 2013-11-24
I'm trying to upgrade from J2EE 1.4 Beta 2 to 1.4 Final. When I run a JUnit test that sends e-mail, I get the following error. This worked fine on Beta 2 - and still does if I switch to point to that j2ee.jar in my classpath. It even worked fine on J2EE 1.3.  

Platform: JDK 1.4.2, Windows XP

===============================================================

[junit] Testcase: testSendTextMessage(org.appfuse.common.util.MailUtilTest): Caused an EROR
[junit] com/sun/mail/util/SharedByteArrayInputStream
[junit] java.lang.NoClassDefFoundError: com/sun/mail/util/SharedByteArrayInputStream
[junit] at org.appfuse.common.util.MailUtil.sendMessage(MailUtil.java:77)
[junit] at org.appfuse.common.util.MailUtil.sendTextMessage(MailUtil.java:151)
[junit] at org.appfuse.common.util.MailUtilTest.testSendTextMessage(MailUtilTest.java:59)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

===============================================================

I'm certainly not importing this class:

===============================================================

package org.appfuse.common.util;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.appfuse.common.Constants;

import java.util.Properties;
import java.util.ResourceBundle;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MailUtil extends Object {

===============================================================

But it does look like a classpath conflict or something??

Here is the offending line of code (MailUtil: 77):

===============================================================

Message message = new MimeMessage(getSession());

Where the getSession() method is:

===============================================================
    public static Session getSession() {
        Session session = null;

        try {
            session =
                (Session) new InitialContext().lookup("java:comp/env/" +
                                                      Constants.JNDI_MAIL);
        } catch (NamingException ex) {
            if (log.isDebugEnabled()) {
                log.info("error communicating with JNDI, assuming testcase");
            }

            ResourceBundle mailProps = ResourceBundle.getBundle("mail");

            Properties props = new Properties();
            props.setProperty("mail.debug", mailProps.getString("mail.debug"));
            props.setProperty("mail.transport.protocol",
                              mailProps.getString("mail.transport.protocol"));
            props.setProperty("mail.host", mailProps.getString("mail.host"));
            props.setProperty("mail.user", mailProps.getString("mail.user"));
            props.setProperty("mail.password",
                              mailProps.getString("mail.password"));
            session = Session.getDefaultInstance(props, null);
        }

        return session;
    }

===============================================================
Any help is greatly appreciated.

Matt
0
Comment
Question by:mraible
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
Comment Utility
appears it could be a class conflict.
what is in your classpath and ext directory?
0
 
LVL 1

Author Comment

by:mraible
Comment Utility
I changed from using j2ee.jar in my classpath to activation.jar and mail.jar and this fixed the problem.  
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now