我正在用 jsp servlet 开发一个应用程序。在那使用 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)