Working with Rules On Exchange Server

Managing Rules

Aspose.Email can be used to manage the rules on Exchange Server using the EWSClient class. This class uses Exchange Web Services (EWS), which are available in Exchange Server 2007 and later releases. To show how to manage rules, this article explains how to:

  • Read the rules already on the server.
  • Create a new rule.
  • Update an existing rule.

Microsoft Exchange Server 2010 Service Pack 1 is required for all features described in this article.

A rule is represented by an InboxRule object. Its conditions are kept in a RulePredicates object - for example the senders or the words the subject must contain - and its actions in a RuleActions object - for example the folder to move the message to.

The samples in this article use these headers and namespaces:

#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/InboxRules/InboxRule.h>
#include <Clients/Exchange/InboxRules/RulePredicates.h>
#include <Clients/Exchange/InboxRules/RuleActions.h>
#include <system/array.h>
#include <system/collections/specialized/string_collection.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;

Read Rules

To get all the rules from the Exchange Server:

  1. Connect to an Exchange Server using the IEWSClient class.
  2. Call the IEWSClient->GetInboxRules() method to get all the rules.
  3. In a loop, browse through all the rules and display the rule properties like conditions, actions, and names.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

try
{
    for (SharedPtr<InboxRule> inboxRule : client->GetInboxRules())
    {
        Console::WriteLine(u"Display Name: " + inboxRule->get_DisplayName());

        // The "From Address" condition
        for (SharedPtr<MailAddress> fromAddress : inboxRule->get_Conditions()->get_FromAddresses())
        {
            Console::WriteLine(u"From: " + fromAddress->get_DisplayName() + u" - " + fromAddress->get_Address());
        }

        // The "Subject Contains" condition
        SharedPtr<Collections::Specialized::StringCollection> subjectStrings = inboxRule->get_Conditions()->get_ContainsSubjectStrings();
        for (int32_t i = 0; i < subjectStrings->get_Count(); i++)
        {
            Console::WriteLine(u"Subject contains: " + subjectStrings->idx_get(i));
        }

        // The "Move to Folder" action
        if (!String::IsNullOrEmpty(inboxRule->get_Actions()->get_MoveToFolder()))
        {
            Console::WriteLine(u"Move message to folder: " + inboxRule->get_Actions()->get_MoveToFolder());
        }
    }
}
catch (System::Exception& ex)
{
    Console::WriteLine(ex->get_Message());
}

client->Dispose();

Creating a New Rule

To create a new rule on the Exchange Server, perform the following steps:

  1. Connect to an Exchange Server using the IEWSClient class.
  2. Create a new instance of the InboxRule class and set the following mandatory properties:
    1. DisplayName
    2. Conditions
    3. Actions
  3. Call the IEWSClient->CreateInboxRule() method to create the rule.

The rule below moves the messages from a given sender whose subject contains “ABC” to another folder. The folder is identified by its ID, which is part of the folder information returned by the client. The second argument of the MailAddress constructor, true, skips the syntax check of the address.

SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

SharedPtr<InboxRule> rule = MakeObject<InboxRule>();
rule->set_DisplayName(u"Message from client ABC");

// Conditions: the subject contains "ABC" and the message comes from the given sender
SharedPtr<RulePredicates> conditions = MakeObject<RulePredicates>();
conditions->get_ContainsSubjectStrings()->Add(u"ABC");
conditions->get_FromAddresses()->Add(MakeObject<MailAddress>(u"administrator@ex2010.local", true));
rule->set_Conditions(conditions);

// Action: move the message to a folder
SharedPtr<RuleActions> actions = MakeObject<RuleActions>();
actions->set_MoveToFolder(u"120:AAMkADFjMjNjMmNjLWE3NzgtNGIzNC05OGIyLTAwNTgzNjRhN2EzNgAuAAAAAABbwP+Tkhs0TKx1GMf0D/cPAQD2lptUqri0QqRtJVHwOKJDAAACL5KNAAA=AQAAAA==");
rule->set_Actions(actions);

try
{
    client->CreateInboxRule(rule);
}
catch (System::Exception& ex)
{
    Console::WriteLine(ex->get_Message());
}

client->Dispose();

Updating a Rule

To update a rule on the Exchange Server:

  1. Connect to an Exchange Server using the IEWSClient class.
  2. Call the IEWSClient->GetInboxRules() method to get all the rules.
  3. In a loop, browse through all the rules and find the rule you want to change by its display name.
  4. Update the rule properties.
  5. Call the IEWSClient->UpdateInboxRule() method to update the rule.

The sample replaces the first sender address of the rule created above.

SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

for (SharedPtr<InboxRule> inboxRule : client->GetInboxRules())
{
    Console::WriteLine(u"Display Name: " + inboxRule->get_DisplayName());
    if (inboxRule->get_DisplayName() == u"Message from client ABC")
    {
        Console::WriteLine(u"Updating the rule....");
        inboxRule->get_Conditions()->get_FromAddresses()->idx_set(0, MakeObject<MailAddress>(u"administrator@ex2010.local", true));
        client->UpdateInboxRule(inboxRule);
    }
}

client->Dispose();