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.
Read Rules
To get all the rules from the Exchange Server:
- Connect to an Exchange Server using the IEWSClient class.
- Call the IEWSClient->GetInboxRules() method to get all the rules.
- In a loop, browse through all the rules and display the rule properties like conditions, actions, and names.
The following code snippet shows you how to read rules.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser()); | |
System::Console::WriteLine(u"Connected to Exchange server"); | |
try | |
{ | |
// Get all Inbox Rules | |
System::ArrayPtr<System::SharedPtr<InboxRule>> inboxRules = client->GetInboxRules(); | |
// Display information about each rule | |
for (System::SharedPtr<InboxRule> inboxRule : inboxRules) | |
{ | |
System::Console::WriteLine(System::String(u"Display Name: ") + inboxRule->get_DisplayName()); | |
// Check if there is a "From Address" condition | |
if (inboxRule->get_Conditions()->get_FromAddresses()->get_Count() > 0) | |
{ | |
for (auto fromAddress : System::IterateOver(inboxRule->get_Conditions()->get_FromAddresses())) | |
{ | |
System::Console::WriteLine(System::String(u"From: ") + fromAddress->get_DisplayName() + u" - " + fromAddress->get_Address()); | |
} | |
} | |
// Check if there is a "Subject Contains" condition | |
if (inboxRule->get_Conditions()->get_ContainsSubjectStrings()->get_Count() > 0) | |
{ | |
for (auto subject : System::IterateOver(inboxRule->get_Conditions()->get_ContainsSubjectStrings())) | |
{ | |
System::Console::WriteLine(System::String(u"Subject contains: ") + subject); | |
} | |
} | |
// Check if there is a "Move to Folder" action | |
if (inboxRule->get_Actions()->get_MoveToFolder().get_Length() > 0) | |
{ | |
System::Console::WriteLine(System::String(u"Move message to folder: ") + inboxRule->get_Actions()->get_MoveToFolder()); | |
} | |
} | |
} | |
catch (System::Exception& ex) | |
{ | |
System::Console::WriteLine(ex.get_Message()); | |
} |
Creating a New Rule
To create a new rule on the Exchange Server, perform the following steps:
- Connect to an Exchange Server using the IEWSClient class.
- Create a new instance of the InboxRule class and set the following mandatory properties:
- DisplayName
- Conditions
- Actions
- Call the IEWSClient->CreateInboxRule() method to create the rule.
The following code snippet shows you how to create a new rule.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
// Set Exchange Server 2010 web service URL, Username, password, domain information | |
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser()); | |
System::Console::WriteLine(u"Connected to Exchange server"); | |
System::SharedPtr<InboxRule> rule = System::MakeObject<InboxRule>(); | |
rule->set_DisplayName(u"Message from client ABC"); | |
// Add conditions | |
System::SharedPtr<RulePredicates> newRules = System::MakeObject<RulePredicates>(); | |
// Set Subject contains string "ABC" and Add the conditions | |
newRules->get_ContainsSubjectStrings()->Add(u"ABC"); | |
newRules->get_FromAddresses()->Add(System::MakeObject<MailAddress>(u"administrator@ex2010.local", true)); | |
rule->set_Conditions(newRules); | |
// Add Actions and Move the message to a folder | |
System::SharedPtr<RuleActions> newActions = System::MakeObject<RuleActions>(); | |
newActions->set_MoveToFolder(u"120:AAMkADFjMjNjMmNjLWE3NzgtNGIzNC05OGIyLTAwNTgzNjRhN2EzNgAuAAAAAABbwP+Tkhs0TKx1GMf0D/cPAQD2lptUqri0QqRtJVHwOKJDAAACL5KNAAA=AQAAAA=="); | |
rule->set_Actions(newActions); | |
try | |
{ | |
client->CreateInboxRule(rule); | |
} | |
catch (System::Exception& ex) | |
{ | |
System::Console::WriteLine(ex.get_Message()); | |
} |
Updating a Rule
To update a rule on the Exchange Server:
- Connect to an Exchange Server using the IEWSClient class.
- Call the IEWSClient->GetInboxRules() method to get all the rules.
- In a loop, browse through all the rules and get the rule you want to change by matching the DisplayName in a condition.
- Update the rule properties
- Call the IEWSClient.UpdateInboxRule() method to update the rule.
The following code snippet shows you how to update a rule.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
// Set mailboxURI, Username, password, domain information | |
System::String mailboxURI = u"https://ex2010/ews/exchange.asmx"; | |
System::String username = u"test.exchange"; | |
System::String password = u"pwd"; | |
System::String domain = u"ex2010.local"; | |
// Connect to the Exchange Server | |
System::SharedPtr<System::Net::NetworkCredential> credential = System::MakeObject<System::Net::NetworkCredential>(username, password, domain); | |
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser()); | |
System::Console::WriteLine(u"Connected to Exchange server"); | |
// Get all Inbox Rules | |
System::ArrayPtr<System::SharedPtr<InboxRule>> inboxRules = client->GetInboxRules(); | |
// Loop through each rule | |
for (System::SharedPtr<InboxRule> inboxRule : inboxRules) | |
{ | |
System::Console::WriteLine(System::String(u"Display Name: ") + inboxRule->get_DisplayName()); | |
if (inboxRule->get_DisplayName() == u"Message from client ABC") | |
{ | |
System::Console::WriteLine(u"Updating the rule...."); | |
inboxRule->get_Conditions()->get_FromAddresses()->idx_set(0, System::MakeObject<MailAddress>(u"administrator@ex2010.local", true)); | |
client->UpdateInboxRule(inboxRule); | |
} | |
} |