eclipse java.lang.ClassNotFoundException: javax.mail.MessagingException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29101542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:29:52  来源:igfitidea点击:

java.lang.ClassNotFoundException: javax.mail.MessagingException

javaeclipsetomcatjakarta-eejavamail

提问by Aditya

I want to send a email from my gmail account to the another mail account using eclipse.I used apache tomcat 7.0.34 as my web server and use port 8080 for apache server(HTTP/1.1) and use JRE 7 as system library and also include mail.jar and activation.jar in Libraries under Java Resources.

我想使用 eclipse 将一封电子邮件从我的 gmail 帐户发送到另一个邮件帐户。我使用 apache tomcat 7.0.34 作为我的 Web 服务器并使用端口 8080 作为 apache 服务器(HTTP/1.1)并使用 JRE 7 作为系统库,并且在 Java 资源下的库中包含 mail.jar 和 activation.jar。

In the below my jsp page is given,where the recipient address is entered and submit to the server.

在下面给出了我的jsp页面,其中输入了收件人地址并提交给服务器。

SendMail.jsp

发送邮件.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <form action="ControllerMail">
            Recipient Address : <input type="email" name="to" ><br/><br/>
            <input type="submit" value="Submit"/>
            <input type="hidden" name="pageType" value="sendmail"/>
        </form>
    </body>
</html>

Now, My servlet class is given below.

现在,下面给出了我的 servlet 类。

ControllerMail.java

控制器邮件.java

package com.sendmail.controller;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ControllerMail extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ControllerMail() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doProcess(request,response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doProcess(request,response);
    }

    protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        String pageType = request.getParameter("pageType");
        if(pageType.equals("SendMail"))
        {
            String to = request.getParameter("to");

            //Get the session object
              Properties props = new Properties();
              props.put("mail.smtp.host", "localhost");
              props.put("mail.smtp.socketFactory.port", "465");
              props.put("mail.smtp.socketFactory.class",
                        "javax.net.ssl.SSLSocketFactory");
              props.put("mail.smtp.auth", "true");
              props.put("mail.smtp.port", "465");

              Session session = Session.getDefaultInstance(props,
               new javax.mail.Authenticator() {
               protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication("********@gmail.com","********");//change accordingly
               }
              });

            //compose message
              try {
               MimeMessage message = new MimeMessage(session);
               message.setFrom(new InternetAddress("********@gmail.com"));//change accordingly
               message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
               message.setSubject("Greetings");
               message.setContent("Hi,what's up","text/html");

               //send message
               Transport.send(message);

               System.out.println("message sent successfully");

              } catch (MessagingException e) {throw new RuntimeException(e);}
        }
    }

}

But when I run my project on server that mean when I right click on my SendMail.jsp page and click on "run on server",the following errors are occured.

但是当我在服务器上运行我的项目时,这意味着当我右键单击我的 SendMail.jsp 页面并单击“在服务器上运行”时,会发生以下错误。

Mar 17, 2015 7:41:38 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\Program Files\PC Connectivity Solution\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_75\bin;F:\Software\JavaMailAPI\activation-jaf1.1.1.jar;F:\Software\JavaMailAPI\java-mail-1.4.4.jar;F:\Software\eclipse-jee-luna-SR1a-win32\eclipse;;.
Mar 17, 2015 7:41:39 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SendEmail' did not find a matching property.
Mar 17, 2015 7:41:39 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8081"]
Mar 17, 2015 7:41:39 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 17, 2015 7:41:39 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 539 ms
Mar 17, 2015 7:41:39 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 17, 2015 7:41:39 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.34
Mar 17, 2015 7:41:39 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SendEmail]]
    at java.util.concurrent.FutureTask.report(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SendEmail]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 6 more
Caused by: java.lang.NoClassDefFoundError: javax/mail/MessagingException
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Unknown Source)
    at java.lang.Class.getDeclaredFields(Unknown Source)
    at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:87)
    at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:261)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:140)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:67)
    at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:405)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:881)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5173)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    ... 20 more

Mar 17, 2015 7:41:39 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
    at java.util.concurrent.FutureTask.report(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:684)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:451)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more

Mar 17, 2015 7:41:39 PM org.apache.catalina.startup.Catalina start
SEVERE: Catalina.start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:684)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:451)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 9 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 11 more

Mar 17, 2015 7:41:39 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 249 ms

After 45 seconds,there will show a dialog box and it displays "Starting Tomcatv7.0 Server at localhost" encountered a problem.

45秒后,会出现一个对话框,显示“Starting Tomcatv7.0 Server at localhost”遇到问题。

回答by MrSimpleMind

java.lang.NoClassDefFoundError: javax/mail/MessagingException

java.lang.NoClassDefFoundError: javax/mail/MessagingException

Add the mail.jarfor example in your tomcat lib (/catalina_home/commons/libs/) or your web application file (see web-inf/lib).

例如,在您的 tomcat 库 (/catalina_home/commons/libs/) 或您的 Web 应用程序文件(请参阅 web-inf/lib)中添加mail.jar

Also you might need the activation jar.

您也可能需要激活 jar

For latest javax.mail.jar see https://java.net/projects/javamail/pages/Home

有关最新的 javax.mail.jar,请参阅https://java.net/projects/javamail/pages/Home

回答by Jens

You miss javax.mail-<version>.jarin your classpath. Download it and add it to your classpath. It is possible that you miss also the dependencies of this jar.

你错过javax.mail-<version>.jar了你的类路径。下载它并将其添加到您的类路径中。您也可能错过了这个 jar 的依赖项。