Trabajando con Reglas en Exchange Server

Gestionando Reglas

Aspose.Email se puede utilizar para gestionar las reglas en Exchange Server utilizando la clase EWSClient. Esta clase utiliza Exchange Web Services (EWS), que están disponibles en Exchange Server 2007 y versiones posteriores. Para mostrar cómo gestionar reglas, este artículo explica cómo:

  • 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:

  1. Conectarse a un Exchange Server utilizando la clase IEWSClient.
  2. Llamar al método IEWSClient->GetInboxRules() para obtener todas las reglas.
  3. En un bucle, recorrer todas las reglas y mostrar las propiedades de la regla como condiciones, acciones y nombres.

El siguiente fragmento de código muestra cómo leer reglas.

// 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());
}

Crear una Nueva Regla

Para crear una nueva regla en el Exchange Server, realiza los siguientes pasos:

  1. Conectarse a un Exchange Server utilizando la clase IEWSClient.
  2. Crear una nueva instancia de la clase InboxRule y establecer las siguientes propiedades obligatorias:
    1. DisplayName
    2. Conditions
    3. Actions
  3. Llamar al método IEWSClient->CreateInboxRule() para crear la regla.

El siguiente fragmento de código muestra cómo crear una nueva regla.

// 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());
}

Actualizar una Regla

Para actualizar una regla en el Exchange Server:

  1. Conectarse a un Exchange Server utilizando la clase IEWSClient.
  2. Llamar al método IEWSClient->GetInboxRules() para obtener todas las reglas.
  3. En un bucle, recorrer todas las reglas y obtener la regla que quieres cambiar emparejando el DisplayName en una condición.
  4. Actualizar las propiedades de la regla.
  5. Llamar al método IEWSClient.UpdateInboxRule() para actualizar la regla.

El siguiente fragmento de código muestra cómo actualizar una regla.

// 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);
}
}