dascolagi
dascolagi

Reputation: 157

JavaMail Folder search() method returns multiple messages for a unique MessageID

JavaMail Folder.search() returns multiple IMAP messages even if a MessageIDTerm is passed to the method. It seems to happen when the messages are subsequent forwards of an orginal message. JavaMail version is 1.4.4. The mail server is MS Exchange 2013. Users send emails with MS Outlook. Here is the code:

            MessageIDTerm messageIDTerm = new MessageIDTerm(uniqueMessageID);
            Message[] messages = folder.search(messageIDTerm);

If the uniqueMessageID is a message ID of an email the has been forwarded, the messages array will contain the message with uniqueMessageID and all the subsequent forwarded messages. Is this behaviour correct? Is there any way to get only the message with the messagedID passed to the search method?

Upvotes: 0

Views: 1894

Answers (2)

Bill Shannon
Bill Shannon

Reputation: 29961

Most likely it's a bug in Exchange. Turn on JavaMail Session debugging and it should provide enough information for you to report the bug to Microsoft.

Are the forwarded messages being sent as attachments to a new message? If so, Exchange may be searching for the header in the attachments as well as the main message, which would be wrong.

BTW, you might want to upgrade to the current 1.5.3 version of JavaMail.

Upvotes: 1

Gordon
Gordon

Reputation: 69

To get only the Messages try this

public void loadFolder() {
    if (store != null && store.isConnected()) {
       try {
            final Folder inbox = store.getFolder("foldername");
            inbox.open(Folder.READ_ONLY);
            final FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.USER), false);
            final Message[] messages = inbox.search(ft);

        for (Message message : messages) {
            try {
                if (message.getContentType().contains("text")) {
                    final String text = (String) message.getContent();
                    System.out.println(text);
                 }
                } catch (Exception e) {
                    LOG.error("", e);
            }
            }
        }

With the Flag you can specify your search

Upvotes: 0

Related Questions