Praca z regułami na serwerze Exchange

Zarządzanie regułami

Aspose.Email for Java może być używany do zarządzania regułami na serwerze Exchange przy użyciu EWSClient klasa. Ta klasa używa Exchange Web Services (EWS), które są dostępne w Exchange Server 2007 i późniejszych wersjach. Aby pokazać, jak zarządzać regułami, ten artykuł wyjaśnia, jak:

  • Odczytaj reguły już istniejące na serwerze.
  • Utwórz nową regułę.
  • Zaktualizuj istniejącą regułę.

Microsoft Exchange Server 2010 Service Pack 1 jest wymagany dla wszystkich funkcji opisanych w tym artykule.

Odczyt reguł

Aby pobrać wszystkie reguły z serwera Exchange:

  1. Połącz się z serwerem Exchange przy użyciu klasy IEWSClient.
  2. Wywołaj metodę IEWSClient.getInboxRules(), aby pobrać wszystkie reguły.
  3. W pętli foreach przeglądaj wszystkie reguły i wyświetlaj ich właściwości, takie jak warunki, akcje i nazwa.

Poniższy fragment kodu pokazuje, jak odczytać reguły.

IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);

System.out.println("Connected to Exchange server");

// Get all Inbox Rules
InboxRule[] inboxRules = client.getInboxRules();

// Display information about each rule
for (InboxRule inboxRule : inboxRules) {
    System.out.println("Display Name: " + inboxRule.getDisplayName());

    // Check if there is a "From Address" condition
    if (inboxRule.getConditions().getFromAddresses().size() > 0) {
        for (MailAddress fromAddress : (Iterable<MailAddress>) inboxRule.getConditions().getFromAddresses()) {
            System.out.println("From: " + fromAddress.getDisplayName() + " - " + fromAddress.getAddress());
        }
    }
    // Check if there is a "Subject Contains" condition
    if (inboxRule.getConditions().containsSubjectStrings().size() > 0) {
        // foreach to while statements conversion
        for (String subject : inboxRule.getConditions().containsSubjectStrings()) {
            System.out.println("Subject contains: " + subject);
        }
    }
    // Check if there is a "Move to Folder" action
    if (inboxRule.getActions().getMoveToFolder().length() > 0) {
        System.out.println("Move message to folder: " + inboxRule.getActions().getMoveToFolder());
    }
}

Tworzenie nowej reguły

Aby utworzyć nową regułę na serwerze Exchange, wykonaj następujące kroki:

  1. Połącz się z serwerem Exchange przy użyciu klasy IEWSClient.
  2. Utwórz nową instancję klasy InboxRule i ustaw następujące obowiązkowe właściwości:
    1. Nazwa wyświetlana
    2. Warunki
    3. Akcje
  3. Wywołaj metodę IEWSClient.createInboxRule(), aby utworzyć regułę.

Poniższy fragment kodu pokazuje, jak utworzyć nową regułę.

IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);

System.out.println("Connected to Exchange server");

InboxRule rule = new InboxRule();
rule.setDisplayName("Message from client ABC");

// Add conditions
RulePredicates newRules = new RulePredicates();
// Set Subject contains string "ABC" and Add the conditions
newRules.containsSubjectStrings().addItem("ABC");
newRules.getFromAddresses().addMailAddress(new MailAddress("administrator@ex2010.local", true));
rule.setConditions(newRules);

// Add Actions and Move the message to a folder
RuleActions newActions = new RuleActions();
newActions.setMoveToFolder("120:AAMkADFjMjNjMmNjLWE3NzgtNGIzNC05OGIyLTAwNTgzNjRhN2EzNgAuAAAAAABbwP+Tkhs0TKx1GMf0D/cPAQD2lptUqri0QqRtJVHwOKJDAAACL5KNAAA=AQAAAA==");
rule.setActions(newActions);
client.createInboxRule(rule);

Aktualizacja reguły

Aby zaktualizować regułę na serwerze Exchange:

  1. Połącz się z serwerem Exchange przy użyciu klasy IEWSClient.
  2. Wywołaj metodę IEWSClient.getInboxRules(), aby pobrać wszystkie reguły.
  3. W pętli foreach przeglądaj wszystkie reguły i uzyskaj regułę, którą chcesz zmienić, dopasowując DisplayName w warunku.
  4. Zaktualizuj właściwości reguły
  5. Wywołaj metodę IEWSClient.updateInboxRule(), aby zaktualizować regułę.

Poniższy fragment kodu pokazuje, jak zaktualizować regułę.

IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);

System.out.println("Connected to Exchange server");

// Get all Inbox Rules
InboxRule[] inboxRules = client.getInboxRules();

// Loop through each rule
for (InboxRule inboxRule : inboxRules) {
    System.out.println("Display Name: " + inboxRule.getDisplayName());
    if ("Message from client ABC".equals(inboxRule.getDisplayName())) {
        System.out.println("Updating the rule....");
        inboxRule.getConditions().getFromAddresses().set_Item(0, new MailAddress("administrator@ex2010.local", true));
        client.updateInboxRule(inboxRule);
    }
}