Filter Emails from Mail Server
Filter Messages by Sender, Recipient or Date
The Pop3Client class, described in Connect to POP3 Server, provides the ListMessages() method which gets all the messages from a mailbox. To get only messages which match some condition, use the overloaded ListMessages() method which takes MailQuery as an argument. The MailQuery class provides various properties for specifying the query conditions, for example, date, subject, sender, recipient and so on. The MailQueryBuilder class is used to build the search expression. First, all the conditions and constraints are set and then MailQuery is filled with the query developed by MailQueryBuilder. The MailQuery class object is used by Pop3Client to extract the filtered information from the server. This article shows how to filter email messages from a mailbox. The first example illustrates how to filter messages based on date and subject. We also show how to filter on other criteria and how to build more complex queries. It also shows the application of Date and Time filter to retrieve the specific emails from the mailbox. In addition, it also shows how to apply case-sensitive filtering.
Filter Messages from Mailbox
To filter messages from a mailbox:
- Connect to POP3 server.
- Create an instance of MailQuery and set the desired properties.
- Call the Pop3Client.ListMessages(MailQuery query) method and pass the MailQuery in parameters to get the filtered messages only.
The following code snippet shows you how to connect to a POP3 mailbox and get messages that arrived today and have the word “newsletter” in the subject.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Connect and log in to POP3 | |
const string host = "host"; | |
const int port = 110; | |
const string username = "user@host.com"; | |
const string password = "password"; | |
Pop3Client client = new Pop3Client(host, port, username, password); | |
// Set conditions, Subject contains "Newsletter", Emails that arrived today | |
MailQueryBuilder builder = new MailQueryBuilder(); | |
builder.Subject.Contains("Newsletter"); | |
builder.InternalDate.On(DateTime.Now); | |
// Build the query and Get list of messages | |
MailQuery query = builder.GetQuery(); | |
Pop3MessageInfoCollection messages = client.ListMessages(query); | |
Console.WriteLine("Pop3: " + messages.Count + " message(s) found."); |
Retrieve Messages by Specific Criteria
The code samples above shows how you can filter messages based on the email subject and date. We can use other properties to set other supported conditions as well. Below are some examples of setting the conditions using MailQuery.
The code snippets that follow show how to filter emails on other criteria:
- Find emails delivered today.
- Find emails received within a range.
- Find emails from a specific sender.
- Find emails sent from a specific domain.
- Find emails sent to a specific recipient.
Today’s Date
The following code snippet shows you how to find emails delivered today.
// 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); |
Date Range
The following code snippet shows you how to find emails received within a 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)); |
Specific Sender
The following code snippet shows you how to find emails from 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"); |
Specific Domain
The following code snippet shows you how to find emails sent from 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"); |
Specific Recipient
The following code snippet shows you how to find emails sent to 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"); |
Build Complex Queries
If different MailQueryBuilder properties are set in separate statements, then all the conditions would be matched. For example, if we want to get messages between a date range and from a specific host, we need to write three statements.
Combine 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)); |
Combine Queries with OR
MailQueryBuilder provides the Or() method which takes two MailQuery instances as parameters. It gets the messages that match any of the two conditions specified. The following code snippet shows how to filter messages that either have “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 Filters
The API also provides the capability to filter emails from the mailbox based on a case sensitive criteria. The following methods provide the capability to search emails specifying case sensitive flag.
- Method Aspose.Email.StringComparisonField.Contains(string value, bool ignoreCase)
- Method Aspose.Email.StringComparisonField.Equals(string value, bool ignoreCase)
- Method Aspose.Email.StringComparisonField.NotContains(string value, bool ignoreCase)
- Method Aspose.Email.StringComparisonField.NotEquals(string value, bool ignoreCase)
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// IgnoreCase is True | |
MailQueryBuilder builder1 = new MailQueryBuilder(); | |
builder1.From.Contains("tesT", true); | |
MailQuery query1 = builder1.GetQuery(); | |
Pop3MessageInfoCollection messageInfoCol1 = client.ListMessages(query1); |