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.

user3014926
  • 1,061
  • 7
  • 18
  • 26
  • You seem to be missing the appropriate Java EE libraries. – Sotirios Delimanolis Jan 14 '14 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 '14 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. – andersschuller Jan 14 '14 at 21:40
  • Yes and I am using maven too. – user3014926 Jan 14 '14 at 21:42
  • Is javamail on your classpath? – hd1 Jan 14 '14 at 21:44
  • Yes, I added C:\Users\Weiqian Wu\Desktop\javax.mail.jar to classpath in enviromental variables. – user3014926 Jan 14 '14 at 21:47
  • Not on the enviromental variables, it should be on the classpath of your container – Jorge Campos Jan 14 '14 at 21:51
  • I just added to my container. – user3014926 Jan 14 '14 at 21:53
  • So when you select your project -> Right click on it ->Build path -> click on Configure Build Path , Do you see mail.jar in your libraries?if yes, then you should probably check the value of customerEmail which you are getting from request parameters. (assuming you are using Eclipse) – Ashish Jan 14 '14 at 22:03
  • I am using IntelliJ though. I added activation.jar and put dependency to pom.xml. It still gives me java.lang.NoClassDefFoundError: Could not initialize class javax.mail.internet.InternetAddress. Can anyone tell me what went wrong? – user3014926 Jan 14 '14 at 22:14

3 Answers3

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.

Community
  • 1
  • 1
GlenPeterson
  • 4,866
  • 5
  • 41
  • 49
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.

Erhard Siegl
  • 557
  • 2
  • 8
  • What do you mean by "which server you are using"? – user3014926 Jan 14 '14 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? – Erhard Siegl Jan 15 '14 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... :-)

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40