0

嗨,我有一个类 sendemail,它有一个方法 sendmail。我有另一个 jsp 页面激活,在激活 jsp 页面中,我调用 sendmail 方法将电子邮件发送给特定的人。出于测试目的,我在 sendEmail 类中创建了一个邮件方法,它没有任何错误,但是当我在 jsp 中使用它时,它显示错误。请帮助我

发送邮件.java

package mail;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendEmail {
    public void sendmail(String Email)
    {
        final String username = "from";
        final String password = "x";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(Email));
            message.setSubject("Activatiozdfdfgen");
            message.setText("Dear user ,"
                + "\n\n your username is xxx and pasword is yyy");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String s[])
    {
        SendEmail se=new SendEmail();
        se.sendmail("to");
    }
    }

这是activation.jsp

<%@ page import="java.sql.*" %><%@ page import="DB.*" %>
<%@ page import="mail.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!Connection con=null;
int uid;%>
<%DataBaseConnection db= new DataBaseConnection();
 con=db.connet();
 uid=Integer.parseInt(request.getParameter("hidden"));

    try{
    PreparedStatement pt=con.prepareStatement("update  registration set status='activated' where UID="+uid);
    pt.executeUpdate();
    SendEmail se=new SendEmail();
    se.sendmail(request.getParameter("email"));

    }

catch(Exception e){out.println(e);} %>


    <script type="text/javascript">
    alert("activated");

    </script>
    <jsp:forward page="Admin.jsp"></jsp:forward>

</body>
</html>

这是错误

type Exception report

message An exception occurred processing JSP page /Activate.jsp at line 21

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /Activate.jsp at line 21

18:     try{
19:     PreparedStatement pt=con.prepareStatement("update  registration set status='activated' where UID="+uid);
20:     pt.executeUpdate();
21:     SendEmail se=new SendEmail();
22:     se.sendmail(request.getParameter("email"));
23:     
24:     }


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/mail/MessagingException
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.Activate_jsp._jspService(Activate_jsp.java:112)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.lang.NoClassDefFoundError: javax/mail/MessagingException
    org.apache.jsp.Activate_jsp._jspService(Activate_jsp.java:85)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.lang.ClassNotFoundException: javax.mail.MessagingException
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    org.apache.jsp.Activate_jsp._jspService(Activate_jsp.java:85)
    org.apache.jasper.runtime.HttpJ
4

1 回答 1

1

原因是它MessagingException不在您的 Web 服务器的类路径中:

java.lang.NoClassDefFoundError: javax/mail/MessagingException

您需要将包含此类的 jar 添加到 Web 服务器的类路径中。在网上搜索以找到适合您的 jar(可能是 mail.jar)。您也可以检查这个SO question

于 2013-05-01T08:08:24.707 回答