159

I am using Java Mail API to read and parse emails. It is working fine with Servlet code.

Now I am trying to write a JUnit test case for the same purpose. But on line Session session = Session.getDefaultInstance(properties); I am getting the following exception:

java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
    at javax.mail.Session.initLogger(Session.java:227)
    at javax.mail.Session.<init>(Session.java:212)
    at javax.mail.Session.getDefaultInstance(Session.java:315)
    at javax.mail.Session.getDefaultInstance(Session.java:355)
    at com.vxl.appanalytix.dataload.fromEmail.EmailParser.parseEmailSubject(EmailParser.java:44)
    at com.vxl.appanalytix.controllers.controllerClasses.GenericEmailServiceClass.readEmailAttachments(GenericEmailServiceClass.java:33)
    at com.vxl.appanalytix.dataload.GenericEmailTestCase.test(GenericEmailTestCase.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 35 more

Can anyone tell me where I am going wrong.

abc
  • 23
  • 4
user2215139
  • 1,805
  • 2
  • 18
  • 24

8 Answers8

192

The javax.mail-api artifact is only good for compiling against.

You actually need to run code, so you need a complete implementation of JavaMail API. Use this:

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

NOTE: The version number will probably differ. Check the latest version here.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
135

com.sun.mail.util.MailLogger is part of JavaMail API. It is already included in EE environment (that's why you can use it on your live server), but it is not included in SE environment.

Oracle docs:

The JavaMail API is available as an optional package for use with Java SE platform and is also included in the Java EE platform.

99% that you run your tests in SE environment which means what you have to bother about adding it manually to your classpath when running tests.

If you're using maven add the following dependency (you might want to change version):

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.0</version>
</dependency>
Eugene Loy
  • 12,224
  • 8
  • 53
  • 79
  • 69
    The javax.mail-api artifact is only good for compiling against. You actually need to run code, so you need a complete implementation of JavaMail. Use `com.sun.mailjavax.mail`. – Bill Shannon May 29 '13 at 22:27
  • 4
    No longer works... Could not find com.sun.mail:javax.mail-api:1.6.0. Searched in the following locations: - https://repo.maven.apache.org/maven2/com/sun/mail/javax.mail-api/1.6.0/javax.mail-api-1.6.0.pom - https://jcenter.bintray.com/com/sun/mail/javax.mail-api/1.6.0/javax.mail-api-1.6.0.pom - https://splunk.jfrog.io/splunk/ext-releases-local/com/sun/mail/javax.mail-api/1.6.0/javax.mail-api-1.6.0.pom – ivan.ukr Jan 23 '21 at 19:19
54

I use the following maven dependencies to get java mail working. The first one includes the javax.mail API (with no implementation) and the second one is the SUN implementation of the javax.mail API.

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.5</version>
</dependency>
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.5</version>
</dependency>
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Shane Rowatt
  • 1,951
  • 3
  • 27
  • 44
  • 11
    You don't need to include the api if you have the implementation. – GlenPeterson Jan 08 '16 at 20:48
  • 1
    Good point @GlenPeterson, I didn't realise that from the solution above but when I looked into the com.sun.mail jar the javax.mail api is included in it. – Peter Jun 17 '16 at 19:10
4

Download this JAR and add it to your libraries: http://java.net/projects/javamail/downloads/download/javax.mail.jar

Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
3

Please see http://mvnrepository.com/artifact/javax.mail/mail/, you can download jar or use the maven dependency, depending on your project type. That should pretty much cover it and you won't get a NoClassDefFoundError exception.

Gaurav B
  • 661
  • 8
  • 9
2

Really it's interesting. You need just use javax-mail.jar of "com.sun" not "javax.mail".
dwonload com.sun mail jar

Resul Rzaeeff
  • 448
  • 4
  • 14
  • 30
1

It happens also if your code is expecting Java Mail 1.4 and your jars are Java Mail 1.3. Happened to me when upgraded Pentaho Kettle

Regards

Carlos Rafael Ramirez
  • 5,984
  • 1
  • 29
  • 36
1

I solved this by uninstalling

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.6.2</version>
</dependency>

and installing

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

You cannot use these two packages together as they incur a java.lang.module.ResolutionException - there is some sort of conflict with JUnit packages.

If you're curious about my versions,

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.4.0</version>
        </dependency>
notacorn
  • 3,526
  • 4
  • 30
  • 60