Solved

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

Posted on 2003-12-10
4
993 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
ID: 9916691
appears it could be a class conflict.
what is in your classpath and ext directory?
0
 
LVL 1

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

947 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

19 Experts available now in Live!

Get 1:1 Help Now