Solved

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

Posted on 2003-12-10
4
999 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

856 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