Filter Messages from Exchange Mailbox using WebDav
Filter Messages using Web Dav
To get filtered messages from a mailbox:
- Connect to Exchange server.
- Create an instance of MailQuery and set the desired properties.
- Call the ExchangeClient.listMessages(MailQuery query) method and pass the MailQuery in the parameters to get the filtered messages only.
The code examples below show how to connect to an Exchange mailbox and get messages that have the string “Newsletter” in the subject and were sent today.
Get Messages that Meet Specific Criteria
The code samples above filters messages based on the email subject and date. We can filter on other properties too. Below are some examples of setting the conditions using MailQuery.
Code Samples: Today’s Date
Code Samples: Date Range
Code Samples: Specific Sender
Code Samples: Specific Domain
Code Samples: Specific Recipient
Code Samples: By MessageID
Building Complex Queries
If different QueryBuilder properties are set in separate statement, all the conditions are matched. For example,to get a message in a particular date range and from a specific host, write three statements:
Code Samples: Combining Queries with AND
Code Samples: Combining Queries with OR
QueryBuilder provides the Or() method which takes two MailQuery instances as parameters. It gets messages that match any of the two conditions specified. The example below filters messages that either has the word “test” in the subject or “email@example.com” as the sender.
Case-Sensitive Email Filtering
Emails can be filtered based on case-sensitivity by specifying the IgnoreCase flag in the filter criteria as shown in the following example.