Làm việc với Quy tắc trên Exchange Server

Quản lý Quy tắc

Aspose.Email cho Java có thể được sử dụng để quản lý các quy tắc trên Exchange Server bằng cách sử dụng EWSClient lớp. Lớp này sử dụng Exchange Web Services (EWS), có sẵn trong Exchange Server 2007 và các phiên bản sau. Để minh họa cách quản lý quy tắc, bài viết này giải thích cách:

  • Đọc các quy tắc đã có trên máy chủ.
  • Tạo một quy tắc mới.
  • Cập nhật một quy tắc hiện có.

Microsoft Exchange Server 2010 Service Pack 1 là yêu cầu cho tất cả các tính năng được mô tả trong bài viết này.

Đọc Quy tắc

Để lấy tất cả các quy tắc từ Exchange Server:

  1. Kết nối tới Exchange Server bằng lớp IEWSClient.
  2. Gọi phương thức IEWSClient.getInboxRules() để lấy mọi quy tắc.
  3. Trong một vòng lặp foreach, duyệt qua mọi quy tắc và hiển thị các thuộc tính của quy tắc như điều kiện, hành động và tên.

Đoạn mã sau đây cho bạn thấy cách đọc các quy tắc.

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

Tạo một Quy tắc Mới

Để tạo một quy tắc mới trên Exchange Server, thực hiện các bước sau:

  1. Kết nối tới Exchange Server bằng lớp IEWSClient.
  2. Tạo một thể hiện mới của lớp InboxRule và thiết lập các thuộc tính bắt buộc sau:
    1. Tên hiển thị
    2. Điều kiện
    3. Hành động
  3. Gọi phương thức IEWSClient.createInboxRule() để tạo quy tắc.

Đoạn mã sau đây cho bạn thấy cách tạo một quy tắc mới.

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

Cập nhật một Quy tắc

Để cập nhật một quy tắc trên Exchange Server:

  1. Kết nối tới Exchange Server bằng lớp IEWSClient.
  2. Gọi phương thức IEWSClient.getInboxRules() để lấy mọi quy tắc.
  3. Trong một vòng lặp foreach, duyệt qua tất cả các quy tắc và lấy quy tắc bạn muốn thay đổi bằng cách khớp DisplayName trong một điều kiện.
  4. Cập nhật các thuộc tính của quy tắc
  5. Gọi phương thức IEWSClient.updateInboxRule() để cập nhật quy tắc.

Đoạn mã sau đây cho bạn thấy cách cập nhật một quy tắc.

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