0

我有一个 OSGi 应用程序,它有一个命令行界面和一个 GWT 界面。我有一个单独的包负责发送电子邮件。它使用 Apache Commons 电子邮件。

电子邮件包仅提供一种发送电子邮件的方法,并且所有属性目前都是硬编码的。

当我从命令行发送电子邮件时,它会获得对捆绑包的引用并毫无问题地发送电子邮件。

当我从 GWT 界面发送电子邮件时,它会获取对捆绑包的引用并尝试发送电子邮件,然后抛出以下异常

org.apache.commons.mail.EmailException: Sending the email to the following server failed : mail.interzet.ru:25 at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242) at org.apache.commons.mail.Email.send(Email.java:1267) at com.ardor.email.internal.EmailServiceImpl.send(EmailServiceImpl.java:134) at com.ardor.email.internal.EmailServiceImpl.sendChangeEmail(EmailServiceImpl.java:66) at com.ardor.web.server.CustomerProxyServiceImpl.changeEmail(CustomerProxyServiceImpl.java:93) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569) at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208) at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248) at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62) at javax.servlet.http.HttpServlet.service(HttpServlet.java:755) at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:558) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:488) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:520) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:973) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:417) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:907) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110) at org.eclipse.jetty.server.Server.handle(Server.java:346) at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:442) at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:941) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:801) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:224) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:51) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:586) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:44) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533) at java.lang.Thread.run(Thread.java:662) Caused by: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp at javax.mail.Session.getProvider(Session.java:229) at javax.mail.Session.getTransport(Session.java:338) at javax.mail.Session.getTransport(Session.java:368) at javax.mail.Transport.send(Transport.java:67) at javax.mail.Transport.send(Transport.java:48) at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)

怎么会这样?它正在运行相同的代码

HtmlEmail email = new HtmlEmail();

email.setHostName(SMTP_HOST);
email.setAuthentication(SMTP_USER, SMTP_PASS);

email.setFrom(SMTP_EMAIL, SMTP_NAME);
email.addTo(emailAddress, customerName);
email.setSubject("Subject");
email.setTextMsg("This is my message");
email.setHtmlMsg("This is the HTML");

email.send();

困惑!

4

2 回答 2

0

尝试在发送电子邮件之前插入:

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());   
final Properties props = new Properties();  
props.setProperty("mail.transport.protocol", "smtp");   
props.setProperty("mail.host", SMTP_HOST);  
props.put("mail.smtp.auth", "true");  
props.put("mail.smtp.port", "465");  
props.put("mail.debug", "true");  
props.put("mail.smtp.socketFactory.port", "465");  
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");  
props.put("mail.smtp.socketFactory.fallback", "false");  
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {   
    protected PasswordAuthentication getPasswordAuthentication() {  
        return new PasswordAuthentication(SMTP_USER, SMTP_PASS);  
    }  
});  
于 2012-05-03T09:27:07.360 回答
0

我不完全确定为什么,但事实证明这是一个简单的类路径错误。似乎 OSGi 包可以访问 javax.mail_1.4.0.jar 并且战争无法访问它。

我将它添加到应用程序 WEB-INF/lib 中,它现在可以正常工作了。

对我来说仍然没有意义,为什么它需要访问它,因为 OSGi 捆绑包在不从 GWT 调用时可以访问它

于 2012-05-22T08:10:43.700 回答