Filter Messages From Exchange Mailbox using WebDav
Filtering Messages using WebDav
The ExchangeClient class provides the ListMessages() method which gets all messages from a mailbox. To get only messages which match some condition, use the overloaded ListMessages() method which takes the MailQuery class as an argument. The MailQuery class provides various properties for specifying conditions, for example, date, subject, sender, and recipient. In addition, the API also allows applying case-sensitivity filters for retrieving emails from the mailbox.
Filtering Messages
To get filtered messages from a mailbox:
- Connect to the Exchange server.
- Create an instance of MailQuery and set the desired properties.
- Call the ExchangeClient.ListMessages() method and pass the MailQuery in the parameters to get the filtered messages only.
The following code snippet shows you how to connect to an IMAP mailbox and get messages that have the string “Newsletter” in the subject and were sent today.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
try | |
{ | |
// Connect to Exchange Server | |
const string mailboxUri = "http://exchange-server/Exchange/username"; | |
const string username = "username"; | |
const string password = "password"; | |
const string domain = "domain.com"; | |
NetworkCredential credential = new NetworkCredential(username, password, domain); | |
ExchangeClient client = new ExchangeClient(mailboxUri, credential); | |
// Query building by means of ExchangeQueryBuilder class | |
ExchangeQueryBuilder builder = new ExchangeQueryBuilder(); | |
// Set Subject and Emails that arrived today | |
builder.Subject.Contains("Newsletter"); | |
builder.InternalDate.On(DateTime.Now); | |
MailQuery query = builder.GetQuery(); | |
// Get list of messages | |
ExchangeMessageInfoCollection messages = client.ListMessages(client.MailboxInfo.InboxUri, query, false); | |
Console.WriteLine("Exchange: " + messages.Count + " message(s) found."); | |
// Disconnect from Exchange | |
client.Dispose(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} |
Filter Messages on 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.
Filter Criteria Today’s Date
The following code snippet shows you how to filter all emails on the basis of today’s date.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Emails that arrived today | |
MailQueryBuilder builder = new MailQueryBuilder(); | |
builder.InternalDate.On(DateTime.Now); |
Filter Criteria Date Range
The following code snippet shows you how to filter all emails on the basis of the date range.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Emails that arrived in last 7 days | |
builder.InternalDate.Before(DateTime.Now); | |
builder.InternalDate.Since(DateTime.Now.AddDays(-7)); |
Filter Criteria Specific Sender
The following code snippet shows you how to filter all emails on the basis of a specific sender.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Get emails from specific sender | |
builder.From.Contains("saqib.razzaq@127.0.0.1"); |
Filter Criteria Specific Domain
The following code snippet shows you how to filter all emails on the basis of a specific domain.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Get emails from specific domain | |
builder.From.Contains("SpecificHost.com"); |
Filter Criteria Specific Recipient
The following code snippet shows you how to filter all emails on the basis of a specific recipient.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Get emails sent to specific recipient | |
builder.To.Contains("recipient"); |
Filter Criteria By MessageID
The following code snippet shows you how to filter all emails on the basis of MessageID.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Get email with specific MessageId | |
ExchangeQueryBuilder builder1 = new ExchangeQueryBuilder(); | |
builder1.MessageId.Equals("MessageID"); |
Filter Criteria All Mail Delivery Notifications
The following code snippet shows you how to filter all emails on the basis of all mail delivery notifications.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Get Mail Delivery Notifications | |
builder1 = new ExchangeQueryBuilder(); | |
builder1.ContentClass.Equals(ContentClassType.MDN.ToString()); |
Building Complex Queries
If different ExchangeQueryBuilder properties are set in a 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:
Combining Queries with AND
The following code snippet shows you how to Combine Queries with AND.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Emails from specific host, get all emails that arrived before today and all emails that arrived since 7 days ago | |
builder.From.Contains("SpecificHost.com"); | |
builder.InternalDate.Before(DateTime.Now); | |
builder.InternalDate.Since(DateTime.Now.AddDays(-7)); |
Combining Queries with OR
ExchangeQueryBuilder provides the Or() method which takes two MailQueryinstances as parameters. It gets messages that match any of the two conditions specified. The example below filters messages that either have the word “test” in the subject or “noreply@host.com” as the sender. The following code snippet shows you how to combine queries with OR.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Specify OR condition | |
builder.Or(builder.Subject.Contains("test"), builder.From.Contains("noreply@host.com")); |
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 code snippet.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Set conditions | |
ExchangeQueryBuilder builder = new ExchangeQueryBuilder(); | |
builder.Subject.Contains("Newsletter", true); | |
builder.InternalDate.On(DateTime.Now); | |
MailQuery query = builder.GetQuery(); |