title: Could not load com.sun.mail.handlers.multipart_mixed
body: <p>We have created a listener using Javamail api to read the newly added message in the email inbox.  Once the listener received the new message, we are trying to fetch all the header information as well as content part. For parsing the content, we have written a proper parsing logic and it's working fine for all type of content(text,html,multipart etc.). Here is the parsing code.</p>
  
  <pre><code>private String getText(Part p) throws MessagingException, IOException {
          if (p.isMimeType("text/*")) {
              String s = (String) p.getContent();         
              return s;
          }
  
          if (p.isMimeType("multipart/alternative")) {
              // prefer html text over plain text
              Multipart mp = (Multipart) p.getContent();
              String text = null;
              for (int i = 0; i &lt; mp.getCount(); i++) {
                  Part bp = mp.getBodyPart(i);
                  if (bp.isMimeType("text/plain")) {
                      if (text == null){
                          text = getText(bp);
                          return text;
                      }
  
                  } else if (bp.isMimeType("text/html")) {
                      String s = getText(bp);
                      if (s != null)
                      return s;
  
                  } else {
                      return getText(bp);
                  }
              }
              return text;
          } else if (p.isMimeType("multipart/*")) {
              Multipart mp = (Multipart) p.getContent();
              for (int i = 0; i &lt; mp.getCount(); i++) {
                  String s = getText(mp.getBodyPart(i));
                  if (s != null)
                      return s;
              }
          }
  
          return null;
      }
  </code></pre>
  
  <p>The above listener and parsing, we are running in a web application which is deployed in a tomcat container (version - Apache tomcat 8.0.35). </p>
  
  <p>After some time when we are getting session time out for a IMAP connection, we are restarting the listener by programmatic. Now the listener is able to read the newly added message to the inbox, but the message content parsing is failing the following exception. We have tried multiple option to fix the issue, but didn't get a success. Below is the exception stack trace</p>
  
  <pre><code>25-Mar-2016 14:08:10.158 INFO [JavaMail-EventQueue] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load [com.sun.mail.handlers.multipart_mixed]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
   java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [com.sun.mail.handlers.multipart_mixed]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
      at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1328)
      at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForClassLoading(WebappClassLoaderBase.java:1316)
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1181)
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
      at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:582)
      at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:560)
      at javax.activation.CommandMap.createDataContentHandler(CommandMap.java:221)
      at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:615)
      at javax.activation.DataHandler.getContent(DataHandler.java:542)
      at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1419)
      at omniquo.awe.util.MailUtil.readMailBody(MailUtil.java:152)
      at omniquo.awe.activitiservice.MailListner$MailThreadListener$2.messagesAdded(MailListner.java:165)
      at javax.mail.event.MessageCountEvent.dispatch(MessageCountEvent.java:150)
      at javax.mail.EventQueue.run(EventQueue.java:134)
      at java.lang.Thread.run(Thread.java:745)
  
  Unable to parse Email com.sun.mail.imap.IMAPInputStream cannot be cast to javax.mail.internet.MimeMultipart
  java.lang.ClassCastException: com.sun.mail.imap.IMAPInputStream cannot be cast to javax.mail.internet.MimeMultipart
      at omniquo.awe.util.MailUtil.readMailBody(MailUtil.java:152)
      at omniquo.awe.activitiservice.MailListner$MailThreadListener$2.messagesAdded(MailListner.java:165)
      at javax.mail.event.MessageCountEvent.dispatch(MessageCountEvent.java:150)
      at javax.mail.EventQueue.run(EventQueue.java:134)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>We need a concrete solution to fix this. We are using the following environment and api versions</p>
  
  <p>JDK- 1.8.0_65, 
  Tomcat - 8.0.35, 
  Java mail - 1.5.5, 
  Spring - 4</p>