Filter Messages From Exchange 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 IEWSClient->ListMessages method and pass the MailQuery in the parameters to get the filtered messages only.
The following code snippet shows you how to 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-C | |
try | |
{ | |
// Connect to EWS | |
const System::String mailboxUri = u"https://outlook.office365.com/ews/exchange.asmx"; | |
const System::String username = u"username"; | |
const System::String password = u"password"; | |
const System::String domain = u"domain"; | |
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser()); | |
// Query building by means of ExchangeQueryBuilder class | |
System::SharedPtr<ExchangeQueryBuilder> builder = System::MakeObject<ExchangeQueryBuilder>(); | |
// Set Subject and Emails that arrived today | |
builder->get_Subject()->Contains(u"Newsletter"); | |
builder->get_InternalDate()->On(System::DateTime::get_Now()); | |
System::SharedPtr<MailQuery> query = builder->GetQuery(); | |
// Get list of messages | |
System::SharedPtr<ExchangeMessageInfoCollection> messages = client->ListMessages(client->get_MailboxInfo()->get_InboxUri(), query, false); | |
System::Console::WriteLine(System::String(u"EWS: ") + messages->get_Count() + u" message(s) found."); | |
// Disconnect from EWS | |
client->Dispose(); | |
} | |
catch (System::Exception& ex) | |
{ | |
System::Console::WriteLine(ex.get_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 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-C | |
// Emails that arrived today | |
System::SharedPtr<MailQueryBuilder> builder = System::MakeObject<MailQueryBuilder>(); | |
builder->get_InternalDate()->On(System::DateTime::get_Now()); |
Filter Criteria Date Range
The following code snippet shows you how to filter emails on the basis of a date range.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
// Emails that arrived in last 7 days | |
builder->get_InternalDate()->Before(System::DateTime::get_Now()); | |
builder->get_InternalDate()->Since(System::DateTime::get_Now().AddDays(-7)); |
Filter Criteria Specific Sender
The following code snippet shows you how to filter 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-C | |
// Get emails from specific sender | |
builder->get_From()->Contains(u"saqib.razzaq@127.0.0.1"); |
Filter Criteria Specific Domain
The following code snippet shows you how to filter 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-C | |
// Get emails from specific domain | |
builder->get_From()->Contains(u"SpecificHost.com"); |
Filter Criteria Specific Recipient
The following code snippet shows you how to filter 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-C | |
// Get emails sent to specific recipient | |
builder->get_To()->Contains(u"recipient"); |
Filter Criteria By MessageID
The following code snippet shows you how to filter emails on the basis of MessageID.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
// Get email with specific MessageId | |
System::SharedPtr<ExchangeQueryBuilder> builder1 = System::MakeObject<ExchangeQueryBuilder>(); | |
builder1->get_MessageId()->Equals(u"MessageID"); |
Filter Criteria All Mail Delivery Notifications
The following code snippet shows you how to filter 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-C | |
// Get Mail Delivery Notifications | |
builder1 = System::MakeObject<ExchangeQueryBuilder>(); | |
builder1->get_ContentClass()->Equals(System::ObjectExt::ToString(ContentClassType::get_MDN())); |
Filter by Message Size
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
builder1 = System::MakeObject<ExchangeQueryBuilder>(); | |
builder1->get_ItemSize()->Greater(80000); |
Building Complex Queries
If different MailQueryBuilder 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-C | |
// Emails from specific host, get all emails that arrived before today and all emails that arrived since 7 days ago | |
builder->get_From()->Contains(u"SpecificHost.com"); | |
builder->get_InternalDate()->Before(System::DateTime::get_Now()); | |
builder->get_InternalDate()->Since(System::DateTime::get_Now().AddDays(-7)); |
Combining Queries with OR
MailQueryBuilder 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 “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-C | |
builder->Or(builder->get_Subject()->Contains(u"test"), builder->get_From()->Contains(u"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-C | |
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser()); | |
// Query building by means of ExchangeQueryBuilder class | |
System::SharedPtr<ExchangeQueryBuilder> builder = System::MakeObject<ExchangeQueryBuilder>(); | |
builder->get_Subject()->Contains(u"Newsletter", true); | |
builder->get_InternalDate()->On(System::DateTime::get_Now()); | |
System::SharedPtr<MailQuery> query = builder->GetQuery(); | |
// Get list of messages | |
System::SharedPtr<ExchangeMessageInfoCollection> messages = client->ListMessages(client->get_MailboxInfo()->get_InboxUri(), query, false); | |
System::Console::WriteLine(System::String(u"EWS: ") + messages->get_Count() + u" message(s) found."); | |
// Disconnect from EWS | |
client->Dispose(); |
Filtering Messages with Paging Support
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser()); | |
int32_t itemsPerPage = 5; | |
System::String sGuid = System::ObjectExt::ToString(System::Guid::NewGuid()); | |
System::String str1 = sGuid + u" - " + u"Query 1"; | |
System::String str2 = sGuid + u" - " + u"Query 2"; | |
System::SharedPtr<MailQueryBuilder> queryBuilder1 = System::MakeObject<MailQueryBuilder>(); | |
queryBuilder1->get_Subject()->Contains(str1); | |
System::SharedPtr<MailQuery> query1 = queryBuilder1->GetQuery(); | |
System::SharedPtr<System::Collections::Generic::List<System::SharedPtr<ExchangeMessagePageInfo>>> pages = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<ExchangeMessagePageInfo>>>(); | |
System::SharedPtr<ExchangeMessagePageInfo> pageInfo = client->ListMessagesByPage(client->get_MailboxInfo()->get_InboxUri(), query1, itemsPerPage); | |
pages->Add(pageInfo); | |
int32_t str1Count = pageInfo->get_Items()->get_Count(); | |
while (!pageInfo->get_LastPage()) | |
{ | |
pageInfo = client->ListMessagesByPage(client->get_MailboxInfo()->get_InboxUri(), query1, itemsPerPage, pageInfo->get_PageOffset() + 1); | |
pages->Add(pageInfo); | |
str1Count += pageInfo->get_Items()->get_Count(); | |
} |