Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用java类发送电子邮件时引发错误_Java_Jsp_Servlets_Jakarta Mail - Fatal编程技术网

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

尝试使用java类发送电子邮件时引发错误,java,jsp,servlets,jakarta-mail,Java,Jsp,Servlets,Jakarta Mail,嗨,我有一个类sendmail,它有一个方法sendmail。我有另一个jsp页面激活,在激活jsp页面中,我调用sendmail方法将电子邮件发送给特定的人。出于测试目的,我在sendEmail类中创建了一个mail方法,它工作时没有任何错误,但当我在jsp中使用它时,它显示错误。请帮助我 sendmail.java package mail; import java.util.Properties; import javax.mail.Message; import javax.mail.

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

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

原因是
MessaginException
不在web服务器的类路径中:

java.lang.NoClassDefFoundError: javax/mail/MessagingException
您需要将包含此类的jar添加到web服务器的类路径中。
搜索web以找到适合您的jar(可能是mail.jar)。您也可以对此进行检查。

看起来您缺少一个jar…只要找到了
main()
方法,应用程序就会启动。直到后来,当它第一次尝试加载
MessaginException
时,它才发现没有可供使用的jarthat@iluxa很抱歉我之前的评论。现在它成功了是的,你是对的,我错过了mail.jar,当我把它放在tomcat/lib中时,它成功了。非常感谢
java.lang.NoClassDefFoundError: javax/mail/MessagingException