我的 servlet 中有以下内容doPost
,将记录插入到 postgres 数据库中,然后向用户发送有关购买的电子邮件。我测试了插入,它运行良好,但是当我尝试为发送电子邮件添加代码时,发生异常错误,我不明白为什么。
我什至在一个标准的单独的 java 应用程序中测试了发送电子邮件功能,它工作正常。这是我的代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Start");
HttpSession s = request.getSession(true);
String firstName = (String) s.getAttribute("firstName");
String lastName = (String) s.getAttribute("lastName");
String email = (String) s.getAttribute("email");
String creditCard = (String) s.getAttribute("cCard");
if (s.getAttribute("bookingCart") != null) {
System.out.println(firstName);
if(firstName == null || lastName == null || email == null || creditCard == null) {
response.sendRedirect("MasterController?confirmBooking=true&error=Data+not+valid");
return;
}
bookingDTO booking = (bookingDTO) s.getAttribute("bookingCart");
bookingsDAO bookingsDAO = new JDBCBookingsDAO();
bookingsDAO.confirmPaymentBooking(booking.getId() , email, firstName, lastName, creditCard);
System.out.println("Booking updated");
String msg = "Dear Customer,\n Thnk you for Using Our website \n Please use link below to confirm your Booking\n"+
" ";
// Recipient's email ID needs to be mentioned.
String to = "brice2nic3@gmail.com";
// Sender's email ID needs to be mentioned
String from = "brice2nic3@gmail.com";
// Assuming you are sending email from localhost
String host = "smtp";
System.out.println("Proterpies");
// Get system properties
Properties properties = System.getProperties();
System.out.println("Booking updated1");
// Setup mail server
properties.setProperty("mail.smtp.host", host);
System.out.println("Booking updated2");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
System.out.println("Booking updated3");
try{
System.out.println("Booking updated4");
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
System.out.println("Booking updated5");
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
System.out.println("Booking updated6");
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
System.out.println("Booking updated7");
// Set Subject: header field
message.setSubject("This is the Subject Line!");
System.out.println("Booking updated8");
// Now set the actual message
message.setText("This is actual message");
System.out.println("Booking updated9");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
s.removeAttribute("bookingCart");
s.setAttribute("bookingCart", null);
s.removeAttribute("bookingAmount");
s.setAttribute("bookingAmount", null);
} else {
System.out.println("Booking not updated");
response.sendRedirect("MasterController?retHome=true");
return;
}
System.out.println("redirected to masterController");
response.sendRedirect("MasterController?Message=Booking+Successful");
}
我的异常信息是:
java.lang.NoClassDefFoundError: javax/mail/MessagingException
java.lang.Class.getDeclaredConstructors0(Native Method)
任何帮助将非常感激。