Move Messages from One Folder to Another using WebDav

You can move email messages from one folder to another with the help of the ExchangeClient.moveMessage() method. It takes the parameters:

  • The unique URI of the message which is to be moved.
  • The unique URI of the destination folder.

Move Messages between Folders

The sample code below that moves a message in a mailbox from the Inbox folder to a folder called Processed. In this example, the application:

  1. Reads messages from the Inbox folder.
  2. Processes some of the messages based on some criteria (in this example, we find a keyword in the message subject).
  3. Moves messages which fulfill the given condition to the Processed folder.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String mailboxURI = "http://ex2003/exchange/administrator"; // WebDAV
ExchangeClient client = new ExchangeClient(mailboxURI, "username", "password", "domain");
ExchangeMailboxInfo mailboxInfo = client.getMailboxInfo();
// List all messages from Inbox folder
System.out.println("Listing all messages from Inbox....");
ExchangeMessageInfoCollection msgInfoColl = client.listMessages(mailboxInfo.getInboxUri());
for (ExchangeMessageInfo msgInfo : msgInfoColl) {
// Move message to "Processed" folder, after processing certain messages based on some criteria
if (msgInfo.getSubject() != null && msgInfo.getSubject().contains("process this message") == true) {
// Move it
client.moveMessage(msgInfo, client.getMailboxInfo().getRootUri() + "/Processed/" + msgInfo.getSubject());
System.out.println("Message moved...." + msgInfo.getSubject());
} else {
// Do something else
}
}