2012-05-10 59 views
0

我在我的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 = "[email protected]"; 

       // Sender's email ID needs to be mentioned 
       String from = "[email protected]"; 

       // 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) 

任何幫助,將不勝感激。

+0

你爲什麼不設置您的SMTP用戶名,密碼和端口號到系統屬性? – Apurv

+0

你正在使用哪個服務器? – Apurv

回答

1

原因NoClassDefFoundError是一個特定的類在Classpath中不可用。第三方API mail.jar應該/WEB-INF/lib下在你的web應用程序

您可能正在運行使用jar命令和類程序清單文件的類路徑中沒有定義屬性

+0

javax.mail應該存在於j2ee.jar中,因爲他正在處理基於servlet的應用程序,j2ee.jar應該存在於服務器上... – Apurv

+0

thnks mate那是問題:) –