尝试使用java类发送电子邮件时抛出错误

时间:2013-05-01 08:02:30

标签: java jsp servlets javamail

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

sendemail.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

1 个答案:

答案 0 :(得分:1)

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

java.lang.NoClassDefFoundError: javax/mail/MessagingException

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