我是使用库的新手,但是我需要能够向忘记密码的用户发送电子邮件。我正在尝试使用JavaMail和Apache Commons Email来实现这一点。但是,我认为我一定是错误地导入了库。
当我尝试发送电子邮件时,收到以下消息:
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库添加到该项目中。这是第一次成功,我能够发送一些成功的电子邮件,但后来它停止了工作(也许是因为我清理并构建了该项目),并且得到了以上消息。
就像我对javamail所做的一样,在commons-email-1.2的bin版本中用jar文件创建了一个新库,并将其添加到项目中。这似乎不起作用,因为当我打开defaultauthenticator.java时,尽管我已经将两个库都添加到了同一netbeans项目中,但它无法导入javax.mail.Authenticator。
将javamail中的mail.jar和所有其他.jars(dsn.jar,imap.jar,pop3.jar等)添加到了与apache common-email-1.2相同的库中,这意味着2个类路径文件(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-库可以从您的Web应用程序中访问,因此它们应该是
mail.jar
activation.jar
夹。