Exception is sometimes being thrown when coppying messages from folder to another using javamail

StackOverflow https://stackoverflow.com//questions/21040020

  •  21-12-2019
  •  | 
  •  

Question

When trying to copy a list of messages from one folder to another, sometime this exceptions appears.

 nested exception is :
 com.sun.mail.iap.BadCommandException: A5 BAD COPY failed. Invalid messageset.
 at com.sun.mail.imap.IMAPFolder.copyMesages(IMAPFolder.java.1769)
 Caused by:
 com.sun.mail.iap.BadCommandException: A5 BAD COPY failed. Invalid messageset.
 at com.sun.mail.iap.Protocol.handleResult(Protocol.java:353)
 at com.sun.mail.iap.Protocol.simpleCommand(Protocol.java:373) ...

My Code :

 public void moveMessageToTrash(String sourceFolder, String destinationFolder,
                         MimeMessage mimeMessage) throws MessagingException
  {
   IMAPFolder source = null;
   IMAPFolder destination = null;
   if(store.isConnected())
    {
     source = (IMAPFolder) store.getFolder(sourceFolder);
     destin= (IMAPFolder) store.getFolder(destinationFolder);
     source.open(Folder.READ_WRITE);
     destin.open(Folder.READ_WRITE);
    }
   Message [] messages = new MimeMessage[1];
   messages[0] = mimeMessage;
   source.copyMessages(messages,destin);
   source.setFlags(messages,new Flags(Flags.Flag.DELETED),true);
   source.expunge():
  }   

EDIT :

Before removing the

   source.expunge();

The thrown exception was :

   messageRemovedException. 

And nothing is being said by the debugger more than what is already said on the initialization of the javamail session :

When the copy fails, nothing is said by the debugger. Everything is shown is only at the initialization of the session :

   INFO : DEBUG : JavaMail version 1.5.1
   INFO : DEBUG : failed to load any providers, using defaults.
   INFO : DEBUG : Tables of loaded providers.
   INFO : DEBUG : Providers Listed By Class Name :     {com.sun.mail.smtp.SMTPSSLTRANSORT=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTRANSPORT,Oracle,1.5.1]....
   INFO : DEBUG : Providers Listed By Protocol :
   ....
   INFO : DEBUG : failed to load address map, using defaults.

what i want to say is that after i removed the source.expunge() from the code, no exception is being thrown anymore, but actually the failure is still exists, some messages are being copied to the target and set deleted in the source, and a litle number of them are copied to the target but set deleted there, not in the source.

Was it helpful?

Solution

Possibly some of the messages you're copying have been deleted and expunged, either by your application or by some other application accessing the same folder? A detailed protocol trace might provide more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top