发送电子邮件异常错误消息

时间:2012-05-10 09:02:26

标签: java email jsp servlets

我的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)

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

NoClassDefFoundError的原因是Classpath中没有特定的类。第三方API mail.jar应位于您的网络应用下的/WEB-INF/lib

您可能正在使用jar命令运行程序,并且未在清单文件的ClassPath属性中定义类