Search code examples
javagmailjakarta-mail

Messages are not moving to the folder in JavaMail


I´m trying to move a array of messages in my application.

But it doesn´t move any messages to folder.

I check with folder methods if isOpen or exists and is true for both methods.

If i create the folder using the inbox. I got inbox\foldername.

But I want to create foldername and move.

So whats wrong?

My folder are create by the sender name. I´m using Gmail.

for (Entry<String, List<Message>> senderAndMessages: mapMessagesBySender.entrySet()) {

    Message []arrayMessages = new Message[senderAndMessages.getValue().size()];
    arrayMessages = senderAndMessages.getValue().toArray(arrayMessages);
    Folder folder = store.getFolder(senderAndMessages.getKey());
    folder.open(Folder.READ_WRITE);

    folder.copyMessages(arrayMessages, folder);

}

Solution

  • You're copying the messages in to the same folder they're already in. The Folder parameter is the destination folder, you need a second Folder object.

    And I can't tell how you're getting the Message objects from the source folder, so you may be doing something wrong there as well.

    Note that the destination folder does not need to be open.