0

私はjspサーブレットで1つのアプリを開発しています。その中で、javamailを使用して、ユーザーにメールを送信しています。私はたくさん試しましたが、例外認証エラーが発生しています。私を助けてください.事前にありがとうコードは以下です-

   package WhackAMole;

  import java.io.IOException;
  import java.util.Properties;
  import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;

public class MailServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String from;
private String to;
private String subject;
private String mess;
private String message;
private String password;

public MailServlet() {
    super();
    // TODO Auto-generated constructor stub
}

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws IOException, ServletException {

     from = request.getParameter("from");
     to = request.getParameter("to");

     subject = "Whack_A_Mole game score";
     mess = request.getParameter("sc");
     message="Your Score is"+mess;
     password =request.getParameter("pass");
    System.out.println("in mail servlet");

    try {

        /* Setting Properties for STMP host */
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.port", "465");

        Authenticator auth = new SMTPAuthenticator(from, password);

        Session session = Session.getInstance(props, auth);
        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setRecipient(Message.RecipientType.TO,
                new InternetAddress(to));
        mimeMessage.setFrom(new InternetAddress(from));
        mimeMessage.setSubject(subject);
        mimeMessage.setText(message);
        Transport.send(mimeMessage);
    } catch (AuthenticationFailedException ex) {
        request.setAttribute("ErrorMessage", "Authentication failed");


    } catch (AddressException ex) {
        request.setAttribute("ErrorMessage", "Wrong email address");

        /*RequestDispatcher dispatcher = request.getRequestDispatcher(err);
        dispatcher.forward(request, response);*/

    } catch (MessagingException ex) {
        request.setAttribute("ErrorMessage", ex.getMessage());

        /*RequestDispatcher dispatcher = request.getRequestDispatcher(err);
        dispatcher.forward(request, response);*/
    }
    /*RequestDispatcher dispatcher = request.getRequestDispatcher(succ);
    dispatcher.forward(request, response);*/

}

private class SMTPAuthenticator extends Authenticator {

    private PasswordAuthentication authentication;

    public SMTPAuthenticator(String login, String password) {
        authentication = new PasswordAuthentication(login, password);
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return authentication;
    }
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
    System.out.println("in servlet...........");
}

   }

エラー-

 java.lang.NoClassDefFoundError: javax/mail/AuthenticationFailedException
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
java.lang.Class.getConstructor0(Unknown Source)
java.lang.Class.newInstance0(Unknown Source)
java.lang.Class.newInstance(Unknown Source)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Unknown Source)
4

2 に答える 2

0

クラスが欠落しているようです。サーバーのlibフォルダーにJavaMailjarを追加してください。

于 2013-01-08T06:26:48.027 に答える
0

2 つの jar を WEB-INF/lib ディレクトリに追加する必要があります。

  1. mail.jar - 実際の smtp 実装が含まれています
  2. activation.jar - mail.jar で必要
于 2013-01-08T07:45:20.843 に答える