Exchange Server'da Kurallarla Çalışma

Kuralları Yönetme

Aspose.Email for Java, Exchange Server’da kuralları yönetmek için kullanılabilir EWSClient sınıfı. Bu sınıf Exchange Web Services (EWS) kullanır; bu hizmetler Exchange Server 2007 ve sonraki sürümlerde mevcuttur. Kuralları nasıl yöneteceğinizi göstermek için bu makale şu adımları açıklar:

  • Sunucuda zaten bulunan kuralları oku.
  • Yeni bir kural oluştur.
  • Mevcut bir kuralı güncelle.

Bu makalede anlatılan tüm özellikler için Microsoft Exchange Server 2010 Service Pack 1 gereklidir.

Kuralları Oku

Exchange Server’dan tüm kuralları almak için:

  1. IEWSClient sınıfını kullanarak bir Exchange Server’a bağlanın.
  2. Tüm kuralları almak için IEWSClient.getInboxRules() metodunu çağırın.
  3. Bir foreach döngüsünde tüm kuralları gezerek, koşullar, eylemler ve isim gibi kural özelliklerini gösterin.

Aşağıdaki kod parçacığı kuralları nasıl okuyacağınızı gösterir.

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

Yeni Kural Oluşturma

Exchange Server’da yeni bir kural oluşturmak için aşağıdaki adımları izleyin:

  1. IEWSClient sınıfını kullanarak bir Exchange Server’a bağlanın.
  2. InboxRule sınıfının yeni bir örneğini oluşturun ve aşağıdaki zorunlu özellikleri ayarlayın:
    1. GörünümAdı
    2. Koşullar
    3. Eylemler
  3. Kuralı oluşturmak için IEWSClient.createInboxRule() metodunu çağırın.

Aşağıdaki kod parçacığı yeni bir kural oluşturmayı gösterir.

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

Kuralı Güncelleme

Exchange Server’da bir kuralı güncellemek için:

  1. IEWSClient sınıfını kullanarak bir Exchange Server’a bağlanın.
  2. Tüm kuralları almak için IEWSClient.getInboxRules() metodunu çağırın.
  3. Bir foreach döngüsünde tüm kuralları gezerek, DisplayName ile eşleşen koşulu sağlayan kuralı bulun ve değiştirin.
  4. Kural özelliklerini güncelle
  5. Kuralı güncellemek için IEWSClient.updateInboxRule() metodunu çağırın.

Aşağıdaki kod parçacığı bir kuralı nasıl güncelleyebileceğinizi gösterir.

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