Trabalhando com Regras no Exchange Server
Gerenciando Regras
Aspose.Email para .NET pode ser usado para gerenciar as regras no Exchange Server usando a classe EWSClient. Esta classe utiliza os Serviços Web do Exchange (EWS), que estão disponíveis no Exchange Server 2007 e versões posteriores. Este artigo explica como gerenciar as regras:
- 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:
- Conecte-se a um Exchange Server usando a classe IEWSClient.
- Chame o método IEWSClient.GetInboxRules() para obter todas as regras.
- Em um loop foreach, navegue por todas as regras e exiba as propriedades da regra, como condições, ações e nome.
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-.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); | |
} | |
} |
Criando uma Nova Regra
Para criar uma nova regra no Exchange Server, execute os seguintes passos:
- Conecte-se a um Exchange Server usando a interface IEWSClient.
- Crie uma nova instância da classe InboxRule e defina as seguintes propriedades obrigatórias:
- DisplayName
- Conditions
- Actions
- 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:
- Conecte-se a um Exchange Server usando a classe IEWSClient.
- Chame o método IEWSClient.GetInboxRules() para obter todas as regras.
- Em um loop foreach, navegue por todas as regras e obtenha a regra que você deseja alterar correspondendo o DisplayName em uma condição.
- Atualize as propriedades da regra.
- Chame o método IEWSClient.UpdateInboxRule() para atualizar a regra.
O seguinte trecho de código mostra como atualizar uma regra.