2012-05-10 47 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那是问题:) –