Tuesday, January 7, 2014

Struggling with NoSuchProviderException: smtp or UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed ?

If you have faced with any of the above exceptions this blog post may be a little help for you to clear out some dark areas as I will list out some helpful posts/forums/articles that I ran through to chase away above two nasty fellows. But the main reason for writing this blog post is as a note to myself how I got through the above scary exceptions. 

I came across the above two exceptions when I was asked to go through this issue. The jaggery pom file that this jira is referring was this. As said in the jira when the jaggery level email feature is enabled, Axis2 email transport is not working and throwing the above said exception of 

ERROR {org.apache.axis2.transport.mail.MailTransportSender} - Error creating mail message or sending it to the configured serverjavax.mail.NoSuchProviderException: smtpat javax.mail.Session.getService(Session.java:782)at javax.mail.Session.getTransport(Session.java:708)at javax.mail.Session.getTransport(Session.java:651)
..............................

As commented in the jira when I remove the below three lines in jaggery pom file under the export package header, Axis2 email transport started to function without any issue.

com.sun.mail.imap;version="2.0.0.wso2v1", 
com.sun.mail.pop3;version="2.0.0.wso2v1", 
com.sun.mail.smtp;version="2.0.0.wso2v1", 

But after removing those lines I tested the jaggery level email feature with a sample application and it had break the jaggery email feature with the same exception as NoSuchProvider found in jaggery Rhino Engine as follows.

ERROR {org.jaggeryjs.scriptengine.engine.RhinoEngine} -  org.mozilla.javascript.WrappedException: Wrapped org.jaggeryjs.scriptengine.exceptions.ScriptException: javax.mail.NoSuchProviderException: smtp (/Test//Test.jag#13)
[2013-12-23 10:09:21,600] ERROR {org.jaggeryjs.jaggery.core.manager.WebAppManager} -  org.mozilla.javascript.WrappedException: Wrapped org.jaggeryjs.scriptengine.exceptions.ScriptException: javax.mail.NoSuchProviderException: smtp (/Test//Test.jag#13)
..........................................

So that made sense when these exports are there, Axis2 email transport picks the wrong bundle at the class loading which is exported from jaggery. ( Where axis2 needs those bundles of version 1.4.0 it picks that of version 2.0.0.wso2v1) 

Then I thought of going ahead with fixing jaggery level email feature to work without the above three exports. Because even though those bundles were exported, they were used no where in jaggery. So there must be some misery why we have exported these in jaggery pom file.

To explore what is going behind the scene I played a bit with the pom file by removing all mail related imports/exports and etc as all those are coming from axis2 email transport. And ended up with the following exception.

ERROR {org.jaggeryjs.scriptengine.engine.RhinoEngine} -  org.mozilla.javascript.WrappedException: Wrapped org.jaggeryjs.scriptengine.exceptions.ScriptException: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed
boundary="----=_Part_0_1755306561.1387772655287" (/Test//Test.jag#13)


So then my journey started to chase after this exception and came across this forum post. Go through the below comment by Oleg which shed some light in to the problem.
(I will add a screen shot of part of the reply for you to identify which one I'm talking about as there are many replies by Oleg )  





Go ahead reading the complete answer by Oleg. Although that answer ensured that this is some problem with class loading in OSGI level and also why we have exported those in jaggery level, still it didn't provide a solution to the problem.

Then this post came in to play.




Now the problem is more clear that this is due to buggy behavior of javax.activation and javax.mail in osgi level, due to issues in dynamic class loading. I could not go with the suggested approach due to some platform specific reasons of our code.

Finally the comments in this post became savior of mine.

So the send code is changed as given below.


         public void jsFunction_send() throws ScriptException { 
             ClassLoader classLoader = Thread.currentThread().getContextClassLoader();                                              Thread.currentThread().setContextClassLoader( javax.mail.Session.class.getClassLoader() );
             try {
                   message.setContent(multipart); 
                  Transport.send(message); 
             } catch (MessagingException e) {
                  throw new ScriptException(e); 
             } finally
                  Thread.currentThread().setContextClassLoader(classLoader);
             } 
      }



Done with the self note :)  In-case you need to go through the current code, my pom file is here and java class is here.

Acknowledgement
Many thanks to Pradeep, Kishanthan and Rajith from WSO2 for their help.


Run Your Tests With GreenMail - A simple Mail Server

Let me start this post with some background information why I had to use a simple mail server like GreenMail.

Background story

I came across the need of an email or some mechanism that can be used in email related testcases. For an example if I need to send an email within my test case what is the mail account that I can use .

Simplest way is create some mail account with G-mail, Yahoo, Gmx or what ever your preferred free email provider and use credentials of that account and provider's smtp severs.

See the example below which is written in Jaggery. You can find a detailed sample code here.

 
var sender = new email.Sender("smtp.gmail.com", "25", "mytestmail@gmail.com", "pw", "tls");


In the above example we have used the smtp mail server of google (smtp.gmail.com) and a some dummy account that we have created with G-mail. Similarly you can do the same with your preferred free email provider.

But the problem with this approach is when you run your test repeatedly for sometime (may be a year or so) test case starts failing. 

Recently I had to go through the same issue and I was amazed when I found out that it is a problem with the credentials that I used in the code which used to be pass the test without any issue earlier. (In the above example "mytestmail@gmail.com" and password "pw") So I tried login to that gmail account and Google complained with the following message.

"Please change your password
We've detected suspicious activity on your Google Account. Please choose a new password to finish signing in. "

Google is suspicious with the account due to reasons listed here and the reason for this particular account may be "Messages that you didn't send appear in Sent Mail ".I could have got this issue fixed by resetting the password of this account as instructed in the above message. But someday I might face the same issue as this is a temporary solution.

So this solution of simple mail servers popped up with this background. Apart from GreenMail, there are many other simple mail servers available such as Apache James, Dumbster and so on. But GreenMail was the one that fitted most in to my requirement and I decided to move on with GreenMail.

How to get GreenMail mail server in your testcase

Add the relevant maven dependency in your pom file. You can find GreenMail dependencies for Maven projects here.

I added following snippet in my pom file.


       <dependency>
            <groupId>com.icegreen</groupId>
            <artifactId>greenmail</artifactId>
            <version>1.3.1b</version>
        </dependency>


And then added the following imports in my test java class file.

   
       import com.icegreen.greenmail.util.GreenMail;
       import com.icegreen.greenmail.util.ServerSetupTest;


Afterwards you have to initialize and start the GreenMail server as stated below in the same test class.


     GreenMail greenMail = new GreenMail ( ServerSetupTest.SMTP );
     greenMail.start ();


Then include what ever your sender code. In my case my sender code is written in Jaggery as below in a separate jaggery (.jag) file. (My test class is pointing to the jaggery file after the Green Mail initialization and start above. )

 
      var from = "from@localhost.com";
      var to = "to@localhost.com";
      var subject = "Test Subject";
      var content = "Test Content";

      var msg = require('email');
      var email = new msg.Sender("localhost", "3025", "from@localhost.com", "frompw", "tls"); 

     email.from = from;
     email.to = to;
     email.subject = subject;
     email.text = content;

     email.send();
     print("email successfully sent");


You may note in the above code that you don't need any free email provider credentials, but simply xxx@localhost.com and some random password. So your test case will not fail due to credential issues.

Also note that without specifying the sender information as in the above code you can also embed that information in your configuration files as described in the answer here.

Your next step is upon message delivery and you need to  check whether the message received is as same as the message sent.

Given below is how you test whether the sent message subject is as same as the received message subject. You can do the same for mail content as well.


     String subject = greenMail.getReceivedMessages()[0].getSubject();
     assertEquals ( subject , "Test Subject");


Donot forget to stop the GreenMail server at the end.


    greenMail.stop();


We are done :) Run your test and see whether your test get passed. 

If you need any reference you can find my pom file here , test class here and my jaggery file here.

Try out more complex test cases with GreenMail !!!