Trabajando con Reglas en Exchange Server
Gestionando Reglas
Aspose.Email para .NET se puede utilizar para gestionar las reglas en Exchange Server utilizando la clase EWSClient. Esta clase utiliza los Servicios Web de Exchange (EWS), que están disponibles en Exchange Server 2007 y versiones posteriores. Este artículo explica cómo gestionar las reglas:
- Leer las reglas ya existentes en el servidor.
- Crear una nueva regla.
- Actualizar una regla existente.
Se requiere Microsoft Exchange Server 2010 Service Pack 1 para todas las funciones descritas en este artículo.
Leer Reglas
Para obtener todas las reglas del Exchange Server:
- Conéctese a un Exchange Server utilizando la clase IEWSClient.
- Llame al método IEWSClient.GetInboxRules() para obtener todas las reglas.
- En un bucle foreach, recorra todas las reglas y muestre las propiedades de la regla, como condiciones, acciones y nombre.
El siguiente fragmento de código le muestra cómo leer reglas.
// 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); | |
} | |
} |
Creando una Nueva Regla
Para crear una nueva regla en el Exchange Server, realice los siguientes pasos:
- Conéctese a un Exchange Server utilizando la interfaz IEWSClient.
- Cree una nueva instancia de la clase InboxRule y establezca las siguientes propiedades obligatorias:
- DisplayName
- Conditions
- Actions
- Llame al método IEWSClient.CreateInboxRule() para crear la regla.
El siguiente fragmento de código le muestra cómo crear una nueva regla.
// 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); |
Actualizando una Regla
Para actualizar una regla en el Exchange Server:
- Conéctese a un Exchange Server utilizando la clase IEWSClient.
- Llame al método IEWSClient.GetInboxRules() para obtener todas las reglas.
- En un bucle foreach, recorra todas las reglas y obtenga la regla que desea cambiar, coincidiendo con el DisplayName en una condición.
- Actualice las propiedades de la regla.
- Llame al método IEWSClient.UpdateInboxRule() para actualizar la regla.
El siguiente fragmento de código le muestra cómo actualizar una regla.
// 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); | |
} | |
} |