0

doPostのサーブレットには、postgres db にレコードを挿入し、購入に関するメールをユーザーに送信する次のものがあります。挿入をテストしたところ、完全に機能しましたが、メール送信用のコードを追加しようとすると例外エラーが発生し、その理由がわかりません。

標準の単独の 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)

どんな助けでも大歓迎です。

4

1 に答える 1

1

理由NoClassDefFoundErrorは、特定のクラスが Classpath で使用できないためです。サードパーティ API は、Web アプリケーションの下にmail.jarある必要があります/WEB-INF/lib

jar コマンドを使用してプログラムを実行していて、クラスがマニフェスト ファイルの ClassPath 属性で定義されていない可能性があります

于 2012-05-10T09:25:56.847 に答える