• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1035
  • Last Modified:

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

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
mraible
Asked:
mraible
1 Solution
 
objectsCommented:
appears it could be a class conflict.
what is in your classpath and ext directory?
0
 
mraibleAuthor Commented:
I changed from using j2ee.jar in my classpath to activation.jar and mail.jar and this fixed the problem.  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now