OSGi GWT org.apache.commons.mail.EmailException

2019-09-08 17:40发布

I have an OSGi application which has a command line interface and a GWT interface. I have a separate bundle that is responsible for sending emails. It uses Apache Commons Email.

The email bundle only provides one method for sending email and all of the properties are hard coded at the moment.

When I send the email from the command line, it gets a reference to the bundle and sends off the email with no problem.

When I send the email from the GWT interface, it gets a reference to the bundle and attempts to send the email and then throws the following exception

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)

How can this be? It is running the same code

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();

Puzzled!

2条回答
forever°为你锁心
2楼-- · 2019-09-08 18:30

Try inserting this before email sending:

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);  
    }  
});  
查看更多
姐就是有狂的资本
3楼-- · 2019-09-08 18:36

I am not completly sure as to why but It turned out to be a simple class path error. It seems the OSGi bundle had access to javax.mail_1.4.0.jar and the war doesn't have access to it.

I added it to the applications WEB-INF/lib and it now works fine.

It still makes no sense to me why it would need access to it as the OSGi bundle can access it when not being called from GWT

查看更多
登录 后发表回答