1

I am trying to send an email using the JavaMail API. Here is my code on the servlet:

package com.lsp.web;

import com.lsp.service.Mailer;
import javax.ejb.EJB;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "contact", urlPatterns = {"/contact"})
public class ContactServlet extends SpringInjectedServlet {
@EJB
private Mailer emailBean;

@Override
public void init() throws ServletException {

}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String customerEmail = req.getParameter("email");
    String subject = req.getParameter("subject");
    String body = req.getParameter("message");

    String error = null;
    String succMess = null;

    try {
        javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
        ia.validate();
        emailBean.send(customerEmail, subject, body);
        req.setAttribute("succMessage", succMess);
        req.getRequestDispatcher("sent.jsp").forward(req, resp);

    } catch (javax.mail.internet.AddressException ae) {
        error = "您指出的邮箱地址不存在";
        req.setAttribute("errorMessage", error);
        req.getRequestDispatcher("contact.jsp").forward(req, resp);
    }
    catch (MessagingException mex) {
        error = "发送失败";
        req.setAttribute("errorMessage", error);
        req.getRequestDispatcher("contact.jsp").forward(req, resp);
    }
}
}

At the line where I check for the user address where:

javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();

I got an exception. java.lang.NoClassDefFoundError: Could not initialize class javax.mail.internet.InternetAddress

In pom.xml, I added these lines:

<!--JavaMail API-->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.5.1</version>
    </dependency>

    <!--EJB-->
    <dependency>
        <groupId>javax.ejb</groupId>
        <artifactId>ejb-api</artifactId>
        <version>3.0</version>
    </dependency>

I am using Tomcat. Could someone tell me why this happens and how I can solve the issue.

Thank you.

10
  • You seem to be missing the appropriate Java EE libraries. Jan 14, 2014 at 21:38
  • How are you compiling and running that code? You must make sure that you have all the necessary libraries in the classpath, not only when compiling, but also when running the code.
    – Jesper
    Jan 14, 2014 at 21:40
  • How are you building and running your application? Are you using Maven? If so, it would probably help if you show us your pom.xml file. Jan 14, 2014 at 21:40
  • Yes and I am using maven too. Jan 14, 2014 at 21:42
  • Is javamail on your classpath?
    – hd1
    Jan 14, 2014 at 21:44

3 Answers 3

2

Please see: https://stackoverflow.com/a/28935760/1128668 You have included the mail-api.jar in your project. That's the API specification only. The fix is to replace this:

<!-- DO NOT USE - it's just the API, not an implementation -->
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>

with the reference implementation of that api:

<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>

I know it has sun in the package name, but that's the latest version.

1

You get a java.lang.NoClassDefFoundError, which means that the JVM can't initialise the class, not that it can't find the class which would be a ClassNotFoundException. A NoClassDefFoundError can be caused by a ClassNotFoundException but that need not be the case.

In order to find the cause, stop the server, delete the log and start again. Then reproduce the error and try to find the first Exception and its cause in your log file. If you are lucky this is the cause for the NoClassDefFoundError.

You also might indicate in your question which server you are using. It might make a difference how to solve the error.

2
  • What do you mean by "which server you are using"? Jan 14, 2014 at 23:03
  • I would guess your emailBean is null as well. You put mail-api and ejb-api into your WAR. But where is the implementation? Tomcat is no EJB container. If you have an EJB container, you shouldn't include the API into your application. The same problem with the mail. You didn't find a Cause for the NoClassDefFoundError? Jan 15, 2014 at 21:52
0

Adding the dependency at build time does nothing to make the dependency available to Tomcat at runtime. You need to add the javax.mail.jar file to the WEB-INF/lib directory of your application, or to Tomcat's lib directory.

Of course, you wouldn't have this problem if you were using a full Java EE application server instead of Tomcat... :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.