Manage Exchange Server Rules with EWS
Aspose.Email for .NET 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. This article explains how to manage the rules:
- 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 foreach loop, browse through all the rules and display the rule properties like conditions, actions, and name.
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-.NET | |
// Set mailboxURI, Username, password, domain information | |
string mailboxURI = "https://ex2010/ews/exchange.asmx"; | |
string username = "test.exchange"; | |
string password = "pwd"; | |
string domain = "ex2010.local"; | |
// Connect to the Exchange Server | |
NetworkCredential credential = new NetworkCredential(username, password, domain); | |
IEWSClient client = EWSClient.GetEWSClient(mailboxURI, credential); | |
Console.WriteLine("Connected to Exchange server"); | |
// Get all Inbox Rules | |
InboxRule[] inboxRules = client.GetInboxRules(); | |
// Display information about each rule | |
foreach (InboxRule inboxRule in inboxRules) | |
{ | |
Console.WriteLine("Display Name: " + inboxRule.DisplayName); | |
// Check if there is a "From Address" condition | |
if (inboxRule.Conditions.FromAddresses.Count > 0) | |
{ | |
foreach (MailAddress fromAddress in inboxRule.Conditions.FromAddresses) | |
{ | |
Console.WriteLine("From: " + fromAddress.DisplayName + " - " + fromAddress.Address); | |
} | |
} | |
// Check if there is a "Subject Contains" condition | |
if (inboxRule.Conditions.ContainsSubjectStrings.Count > 0) | |
{ | |
foreach (String subject in inboxRule.Conditions.ContainsSubjectStrings) | |
{ | |
Console.WriteLine("Subject contains: " + subject); | |
} | |
} | |
// Check if there is a "Move to Folder" action | |
if (inboxRule.Actions.MoveToFolder.Length > 0) | |
{ | |
Console.WriteLine("Move message to folder: " + inboxRule.Actions.MoveToFolder); | |
} | |
} |
Create Rules
To create a new rule on the Exchange Server, perform the following steps:
- Connect to an Exchange Server using the IEWSClient interface.
- 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-.NET | |
// Set Exchange Server web service URL, Username, password, domain information | |
string mailboxURI = "https://ex2010/ews/exchange.asmx"; | |
string username = "test.exchange"; | |
string password = "pwd"; | |
string domain = "ex2010.local"; | |
// Connect to the Exchange Server | |
NetworkCredential credential = new NetworkCredential(username, password, domain); | |
IEWSClient client = EWSClient.GetEWSClient(mailboxURI, credential); | |
Console.WriteLine("Connected to Exchange server"); | |
InboxRule rule = new InboxRule(); | |
rule.DisplayName = "Message from client ABC"; | |
// Add conditions | |
RulePredicates newRules = new RulePredicates(); | |
// Set Subject contains string "ABC" and Add the conditions | |
newRules.ContainsSubjectStrings.Add("ABC"); | |
newRules.FromAddresses.Add(new MailAddress("administrator@ex2010.local", true)); | |
rule.Conditions = newRules; | |
// Add Actions and Move the message to a folder | |
RuleActions newActions = new RuleActions(); | |
newActions.MoveToFolder = "120:AAMkADFjMjNjMmNjLWE3NzgtNGIzNC05OGIyLTAwNTgzNjRhN2EzNgAuAAAAAABbwP+Tkhs0TKx1GMf0D/cPAQD2lptUqri0QqRtJVHwOKJDAAACL5KNAAA=AQAAAA=="; | |
rule.Actions = newActions; | |
client.CreateInboxRule(rule); |
Update Rules
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 foreach 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-.NET | |
// Set mailboxURI, Username, password, domain information | |
string mailboxURI = "https://ex2010/ews/exchange.asmx"; | |
string username = "test.exchange"; | |
string password = "pwd"; | |
string domain = "ex2010.local"; | |
// Connect to the Exchange Server | |
NetworkCredential credential = new NetworkCredential(username, password, domain); | |
IEWSClient client = EWSClient.GetEWSClient(mailboxURI, credential); | |
Console.WriteLine("Connected to Exchange server"); | |
// Get all Inbox Rules | |
InboxRule[] inboxRules = client.GetInboxRules(); | |
// Loop through each rule | |
foreach (InboxRule inboxRule in inboxRules) | |
{ | |
Console.WriteLine("Display Name: " + inboxRule.DisplayName); | |
if (inboxRule.DisplayName == "Message from client ABC") | |
{ | |
Console.WriteLine("Updating the rule...."); | |
inboxRule.Conditions.FromAddresses[0] = new MailAddress("administrator@ex2010.local", true); | |
client.UpdateInboxRule(inboxRule); | |
} | |
} |