Bekerja dengan Aturan di Exchange Server

Mengelola Aturan

Aspose.Email for Java dapat digunakan untuk mengelola aturan di Exchange Server menggunakan EWSClient kelas. Kelas ini menggunakan Exchange Web Services (EWS), yang tersedia di Exchange Server 2007 dan rilis selanjutnya. Untuk menunjukkan cara mengelola aturan, artikel ini menjelaskan cara:

  • Baca aturan yang sudah ada di server.
  • Buat aturan baru.
  • Perbarui aturan yang ada.

Microsoft Exchange Server 2010 Service Pack 1 diperlukan untuk semua fitur yang dijelaskan dalam artikel ini.

Baca Aturan

Untuk mengambil semua aturan dari Exchange Server:

  1. Hubungkan ke Exchange Server menggunakan kelas IEWSClient.
  2. Panggil metode IEWSClient.getInboxRules() untuk mengambil semua aturan.
  3. Dalam loop foreach, jelajahi semua aturan dan tampilkan properti aturan seperti kondisi, tindakan, dan nama.

Potongan kode berikut menunjukkan cara membaca aturan.

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

Membuat Aturan Baru

Untuk membuat aturan baru di Exchange Server, ikuti langkah-langkah berikut:

  1. Hubungkan ke Exchange Server menggunakan kelas IEWSClient.
  2. Buat instansi baru dari kelas InboxRule dan atur properti wajib berikut:
    1. Nama Tampilan
    2. Kondisi
    3. Tindakan
  3. Panggil metode IEWSClient.createInboxRule() untuk membuat aturan.

Potongan kode berikut menunjukkan cara membuat aturan baru.

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

Memperbarui Aturan

Untuk memperbarui aturan di Exchange Server:

  1. Hubungkan ke Exchange Server menggunakan kelas IEWSClient.
  2. Panggil metode IEWSClient.getInboxRules() untuk mengambil semua aturan.
  3. Dalam loop foreach, jelajahi semua aturan dan dapatkan aturan yang ingin diubah dengan mencocokkan DisplayName dalam sebuah kondisi.
  4. Perbarui properti aturan
  5. Panggil metode IEWSClient.updateInboxRule() untuk memperbarui aturan.

Potongan kode berikut menunjukkan cara memperbarui aturan.

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