การทำงานกับกฎบน Exchange Server

การจัดการกฎ

Aspose.Email for Java สามารถใช้จัดการกฎบน Exchange Server โดยใช้ EWSClient คลาส นี้ใช้ Exchange Web Services (EWS) ซึ่งมีใน Exchange Server 2007 และรุ่นต่อมานี้ เพื่อแสดงวิธีจัดการกฎ บทความนี้อธิบายวิธี:

  • อ่านกฎที่มีอยู่แล้วบนเซิร์ฟเวอร์.
  • สร้างกฎใหม่.
  • อัปเดตกฎที่มีอยู่.

Microsoft Exchange Server 2010 Service Pack 1 จำเป็นสำหรับคุณสมบัติทั้งหมดที่อธิบายในบทความนี้

อ่านกฎ

เพื่อรับกฎทั้งหมดจาก Exchange Server:

  1. เชื่อมต่อกับ Exchange Server โดยใช้คลาส IEWSClient
  2. เรียกเมธอด IEWSClient.getInboxRules() เพื่อรับกฎทั้งหมด
  3. ในลูป foreach ให้เรียกดูกฎทั้งหมดและแสดงคุณสมบัติกฎเช่น เงื่อนไข การกระทำ และชื่อ

โค้ดสแนปต่อไปนี้แสดงวิธีอ่านกฎ

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

การสร้างกฎใหม่

เพื่อสร้างกฎใหม่บน Exchange Server ให้ทำตามขั้นตอนต่อไปนี้:

  1. เชื่อมต่อกับ Exchange Server โดยใช้คลาส IEWSClient
  2. สร้างอินสแตนซ์ใหม่ของคลาส InboxRule และตั้งค่าคุณสมบัติบังคับต่อไปนี้:
    1. ชื่อที่แสดง
    2. เงื่อนไข
    3. การกระทำ
  3. เรียกเมธอด IEWSClient.createInboxRule() เพื่อสร้างกฎ

โค้ดสแนปต่อไปนี้แสดงวิธีสร้างกฎใหม่

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

การอัปเดตกฎ

เพื่ออัปเดตกฎบน Exchange Server:

  1. เชื่อมต่อกับ Exchange Server โดยใช้คลาส IEWSClient
  2. เรียกเมธอด IEWSClient.getInboxRules() เพื่อรับกฎทั้งหมด
  3. ในลูป foreach ให้เรียกดูกฎทั้งหมดและเลือกกฎที่ต้องการเปลี่ยนโดยจับคู่ DisplayName ในเงื่อนไข
  4. อัปเดตคุณสมบัติกฎ
  5. เรียกเมธอด IEWSClient.updateInboxRule() เพื่ออัปเดตกฎ

โค้ดสแนปต่อไปนี้แสดงวิธีอัปเดตกฎ

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