Search code examples
javaemailgmailjakarta-mailimap

JavaMail - NoSuchMethodError: javax.mail.internet.ParameterList.combineSegments()V


I need to read (get as string, to be precise) the first message in the Sent Mail folder of a Gmail account, in order to use it's content in automated tests.

I'm using JavaMail to do so, with IMAP protocol:

public static void main(String[] args) {
        String invitationContent = "No invitation";

        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");

        try {
            Session session = Session.getDefaultInstance(props, null);

            Store store = session.getStore();
            store.connect("imap.gmail.com", "no-reply@companyname.com", "somepassword");

            Folder inbox = store.getFolder("[Gmail]/Sent Mail");
            inbox.open(Folder.READ_ONLY);
            Message[] messages = inbox.getMessages();

            Address userAddress = messages[messages.length-1].getAllRecipients()[0];
            System.out.println(userAddress.toString());

            invitationContent = messages[messages.length-1].getContent().toString();

            inbox.close(true);
            store.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(invitationContent);
    }

This code successfully print the userAddress, however when it gets to the message this exception is thrown:

Exception in thread "main" java.lang.NoSuchMethodError: javax.mail.internet.ParameterList.combineSegments()V
    at com.sun.mail.imap.protocol.BODYSTRUCTURE.parseParameters(BODYSTRUCTURE.java:424)
    at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:244)
    at com.sun.mail.imap.protocol.FetchResponse.parseItem(FetchResponse.java:260)
    at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:215)
    at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:96)
    at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:350)
    at com.sun.mail.iap.Protocol.command(Protocol.java:357)
    at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1937)
    at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1929)
    at com.sun.mail.imap.protocol.IMAPProtocol.fetchBodyStructure(IMAPProtocol.java:1504)
    at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1461)
    at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:745)
    at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:927)
    at com.company.Main.main(Main.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

The documentation of the method here indicates that it is being called in order to combine all segments of multi-segment names. I'm pretty sure that the mail doesn't use those. In sake of understanding the problem I sent an email with just 1 "TO" that is a regular gmail address, a simple "test mail" subject and "123" as content, and still got this exception.


Solution

  • You've deployed a different version of the JavaMail Jars from the ones you compiled against, or a set that is internally self-inconsistent. Download latest and start again with a clean build and deploy.