2

I was using mail-1.4.jar version to read gmail inbox. In order to configure listener, I have upgraded it to javax.mail-1.6.0.jar. After this upgrade, email reader method has started throwing this error. Even after lots of google and several trials related to maven dependencies and version changes I am not able to solve this.

java.lang.NoSuchMethodError: javax.mail.internet.ParameterList.combineSegments()V

Below is the maven dependency in my pom.xml

<dependency>
      <groupId>com.sun.mail</groupId>
      <artifactId>javax.mail</artifactId>
      <version>1.6.0</version>
</dependency>

Below the Java code :

Properties props = new Properties();
props.put("mail.store.protocol", "imaps");
Session session = Session.getInstance(props, null);
Store store = session.getStore();
try {
 store.connect("imap.gmail.com", userName, password);
}catch(Exception e){
 e.printStackTrace();
 logger.debug("IMAP Email Listener connection fail");
}

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);

Message[] msgs = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));

for (int i = 0; i < msgs.length; i++) {
  try {
   String subject = msgs[i].getSubject();
   logger.debug("subject " + i + ":" + subject);
   Object content = msgs[i].getContent(); // Here is the exception comes
   logger.debug("content" + i + ":" + content.toString());
  } catch(Exception e) {

  }
}

When I run the same code in stand alone java class everything works fine. But the same code doesn't work when I deploy war file in tomcat.

Stacktrace:

java.lang.NoSuchMethodError: javax.mail.internet.ParameterList.combineSegments()V
 com.sun.mail.imap.protocol.BODYSTRUCTURE.parseParameters(BODYSTRUCTURE.java:420)
 com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:239)
 com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:110)
 com.sun.mail.imap.protocol.FetchResponse.parseItem(FetchResponse.java:256)
 com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:212)
 com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:96)
 com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:409)
 com.sun.mail.iap.Protocol.command(Protocol.java:355)
 com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2151)
 com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2143)
 com.sun.mail.imap.protocol.IMAPProtocol.fetchBodyStructure(IMAPProtocol.java:1717)
 com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1552)
 com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:808)
 javax.mail.internet.MimeMessage.getContent(MimeMessage.java:927)
manurajhada
  • 5,284
  • 3
  • 24
  • 43
  • Please post the full exception stacktrace. Also make sure you have updated all dependencies, as `ParameterList.combineSegments()` was added in JavaMail 1.5, and you previously used JavaMail 1.4. This error could indicate that something in your dependency tree is downgrading to JavaMail 1.4. – Mark Rotteveel Nov 10 '17 at 14:02
  • Updated stacktrace. @MarkRotteveel. Yes I have removed old dependency entry from pom.xml. Also verified that older version is not part of lib folder of war file. – manurajhada Nov 10 '17 at 14:10
  • Does [this](https://stackoverflow.com/questions/31984127/javamail-nosuchmethoderror-javax-mail-internet-parameterlist-combinesegments) provide any help? – pirho Nov 10 '17 at 14:15
  • I have seen this post while searching internet for help. Seen all the possible areas but count fine older version of jar in my war/classpath. :( – manurajhada Nov 12 '17 at 06:11

1 Answers1

2

When I run the same code in stand alone java class everything works fine. But the same code doesn't work when I deploy war file in tomcat.

Probably caused by one of the following:

  • Your Tomcat server is using a previous or improper version of javax.mail. Make sure a javax.mail jar is not somewhere on the CLASSPATH of your server, such as in CATALINA_HOME/lib folder
  • Maybe your app is packaged using an improper version of javax.mail. Check the generated package (WAR file?) to see if it includes the javax.mail jar

EDIT: to easily find from which JAR file your class is loaded from, add a debug output in your app on startup or before the class is supposed to be used such as:

System.out.println(ParameterList.class.getResource("Paramete‌rList.class"));

And deploy your app on the bogus server. It will show you from which (wrong) JAR the class is loaded from.

Pierre B.
  • 11,612
  • 1
  • 37
  • 58
  • There's definitely more than one copy of the JavaMail classes on your classpath, and they're not the same version. – Bill Shannon Nov 11 '17 at 02:19
  • Agree. The only possible reason is having an older version of jar. But I have searched alot. There is no other version of JavaMail in my war's lib folder not even in the Tomcat lib folder. My war file's lib folder contains the only version that I have included in my pom. Is there any other 3rd party library jar that contains java.mail within its package? – manurajhada Nov 12 '17 at 06:17
  • Better yet, add a little debug output in your app (on startup for example) showing from which JAR is loaded your class, something like: `System.out.println(ParameterList.class.getResource("ParameterList.class"));`. Then package and deploy your app on the bogus server to see from which (wrong) JAR your class is loaded ;) I got an output like: `jar:file:/C:/Users/meself/.m2/repository/com/sun/mail/javax.mail/1.6.0/javax.mail-1.6.0.jar!/javax/mail/internet/ParameterList.class` See https://stackoverflow.com/questions/227486/find-where-java-class-is-loaded-from for details – Pierre B. Nov 12 '17 at 11:53
  • @PierreB. Its null. System.out.println(ParameterList.class.getResource("Paramete‌rList.class")); – manurajhada Nov 13 '17 at 06:35
  • Try it with the classloader itself: `ParameterList.class.getClassLoader().getResource("ParameterList.class")`. Otherwise, retrieve `System.getProperty("java.class.path")`and explore each folder one per one. – Pierre B. Nov 13 '17 at 08:35
  • Solved! Google App Engine jar has complete package of javax.mail. And they do not have its jar included, they have all the .class files included in their jar. – manurajhada Nov 14 '17 at 06:46
  • Great! How did you en-up finding the jar, using one of these methods? – Pierre B. Nov 14 '17 at 08:33
  • @manurajhada faced similar issue other lib was loaded from com.google.http-client maybe google-http-client-appengine inside that need to exclude it in pom xml. – Aks Aug 29 '19 at 12:28