Delete Messages from Exchange Server using WebDav

Contents
[ ]

You can delete email messages from a folder with the help of the ExchangeClient.deleteMessage() method. It takes the message’s unique URI as a parameter.

The sample code below deletes a message from the Inbox folder. For the purpose of this example, the code:

  1. Reads messages from the Inbox folder.
  2. Process messages based on some criteria (in this example, we find a keyword in the message subject).
  3. Delete the message.  
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
ExchangeClient client = new ExchangeClient("http://ex2003/exchange/administrator", "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) {
// Delete message based on some criteria
if (msgInfo.getSubject() != null && msgInfo.getSubject().contains("delete") == true) {
// Delete it
client.deleteMessage(msgInfo.getUniqueUri());
System.out.println("Message deleted...." + msgInfo.getSubject());
} else {
// Do something else
}
}