Search code examples
javajakarta-mail

ClassNotFoundException com.sun.mail.util.SharedByteArrayInputStream


I am getting the following error when my deployed system sends an email.

Caused by: java.lang.NoClassDefFoundError: com/sun/mail/util/SharedByteArrayInputStream
....
....
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.SharedByteArrayInputStream
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass

I am using:

<dependency>
   <groupId>com.sun.mail</groupId>
   <artifactId>javax.mail</artifactId>
   <version>1.5.1</version>
</dependency>

This has been working for the past year and now suddenly it is throwing this exception. I have changed any of the versions of javax.mail etc.

I tried adding javax.mail:mail:jar:1.5.0 to the dependencies but when compiling I got an error saying that that jar was illegal. Maven gives the following error: Please use the correct artifact for JavaMail. You should be using com.sun.mail:javax.mail... & Found Banned Dependency: javax.mail:mail:jar:1.5.0

What seems odd to me is that javax.mail.internet.MimeMessage has the following imports:

import com.sun.mail.util.*;
import javax.mail.util.SharedByteArrayInputStream;

So it seems odd that I get this error when creating a MimeMessage in that it is trying to get com.sun.mail.util.SharedByteArrayInputStream instead of javax.mail.util.SharedByteArrayInputStream.

I know similar questions have been posted but the answers always seem to be to use javax.mail.jar which I am already using.


Solution

  • It sounds like you have a very old version of the javax.mail classes on your classpath somewhere. Are you sure there isn't a j2ee.jar or javaee.jar in there somewhere? Look inside all the jar files for javax.mail classes.