Trabalhando com Regras no Exchange Server

Gerenciando Regras

Aspose.Email pode ser usado para gerenciar as regras no Exchange Server usando a classe EWSClient. Esta classe usa os Serviços Web do Exchange (EWS), que estão disponíveis no Exchange Server 2007 e em versões posteriores. Para mostrar como gerenciar regras, este artigo explica como:

  • Ler as regras já no servidor.
  • Criar uma nova regra.
  • Atualizar uma regra existente.

O Microsoft Exchange Server 2010 Service Pack 1 é necessário para todos os recursos descritos neste artigo.

Ler Regras

Para obter todas as regras do Exchange Server:

  1. Conecte-se a um Exchange Server usando a classe IEWSClient.
  2. Chame o método IEWSClient->GetInboxRules() para obter todas as regras.
  3. Em um loop, navegue por todas as regras e exiba as propriedades da regra, como condições, ações e nomes.

O seguinte trecho de código mostra como ler regras.

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

Criando uma Nova Regra

Para criar uma nova regra no Exchange Server, siga os seguintes passos:

  1. Conecte-se a um Exchange Server usando a classe IEWSClient.
  2. Crie uma nova instância da classe InboxRule e defina as seguintes propriedades obrigatórias:
    1. DisplayName
    2. Conditions
    3. Actions
  3. Chame o método IEWSClient->CreateInboxRule() para criar a regra.

O seguinte trecho de código mostra como criar uma nova regra.

Atualizando uma Regra

Para atualizar uma regra no Exchange Server:

  1. Conecte-se a um Exchange Server usando a classe IEWSClient.
  2. Chame o método IEWSClient->GetInboxRules() para obter todas as regras.
  3. Em um loop, navegue por todas as regras e obtenha a regra que você deseja alterar, correspondendo o DisplayName em uma condição.
  4. Atualize as propriedades da regra
  5. Chame o método IEWSClient.UpdateInboxRule() para atualizar a regra.

O seguinte trecho de código mostra como atualizar uma regra.