我不熟悉使用庫,但我需要能夠發送電子郵件給忘記密碼的用戶。我正在嘗試使用JavaMail和Apache Commons Email來實現這一點。但是,我認爲我必須錯誤地導入這些庫。org.apache.jasper.JasperException:無法編譯JSP的類
當我嘗試發送一封電子郵件,我得到以下信息:
Apr 17, 2011 11:54:33 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 60 in the jsp file: /sendpw.jsp
The type javax.mail.Authenticator cannot be resolved. It is indirectly referenced from required .class files
代碼:
57: EmailAuthentication ea = new EmailAuthentication();
58: String authUser = ea.getUser();
59: String authPw = ea.getPw();
60: email.setAuthenticator(new DefaultAuthenticator(authUser, authPw));
61: email.setTLS(true);
62: email.setFrom(authUser+"@gmail.com");
63: email.setSubject("Your Last Love Password");
堆棧跟蹤:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:589)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:662)
我已經試過了幾東西:
將包含所有.java文件的org.apache.commons.mail文件夾複製到我的netbeans項目的源文件中,然後將javamail庫添加到項目中。這是第一次,我能夠發送一些成功的電子郵件,但有些路上它停止工作(也許是因爲我清理和建立了項目),我收到了上述消息。
在commons-email-1.2的bin版本中創建了一個新庫,其jar文件就像我對javamail所做的一樣,並將其添加到項目中。這個似乎不起作用,因爲當我打開defaultauthenticator.java時,它不能導入javax.mail.Authenticator,儘管我已經將兩個庫添加到同一個netbeans項目中。
將javamail中的mail.jar和其他所有.jar(dsn.jar,imap.jar,pop3.jar等)添加到apache common-email-1.2所在的庫中,即2個classpath文件(mail .jar和commons-email-1.2.jar)和6個源文件。這一次,defaultauthenticator.java沒有錯誤,但仍然無法發送任何電子郵件(與上面相同的服務器錯誤)。
涉及代碼的部分是:
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(587);
......
email.setAuthenticator(new DefaultAuthenticator(authUser, authPw));
email.setTLS(true);
email.setFrom(authUser+"@gmail.com");
email.setSubject("Your Password");
....
我使用NetBeans 6.9.1,Tomcat的6.0.26。
我在做什麼錯了?
謝謝!當我將mail.jar和activation.jar放入tomcat6/lib時,它工作正常。我沒有意識到我需要這樣做(因爲我使用的是JDK1.6,所以我沒有做任何關於activation.jar的事情)。它沒有工作,當我把它們都放入我創建的WEB-INF/lib文件夾中時(我首先嚐試了第二個選項)。希望它從現在起繼續順利運行:) – Grace 2011-04-18 04:28:10