Exchange Server에서 규칙 작업

규칙 관리

Aspose.Email for Java는 다음을 사용하여 Exchange Server에서 규칙을 관리할 수 있습니다 EWSClient 클래스. 이 클래스는 Exchange Server 2007 및 이후 릴리스에서 사용할 수 있는 Exchange Web Services (EWS)를 사용합니다. 규칙 관리 방법을 보여주기 위해 이 문서는 다음을 설명합니다:

  • 서버에 이미 존재하는 규칙을 읽습니다.
  • 새 규칙 생성.
  • 기존 규칙 업데이트.

이 문서에 설명된 모든 기능에는 Microsoft Exchange Server 2010 Service Pack 1이 필요합니다.

규칙 읽기

Exchange Server에서 모든 규칙을 가져오려면:

  1. IEWSClient 클래스를 사용하여 Exchange Server에 연결합니다.
  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. IEWSClient 클래스를 사용하여 Exchange Server에 연결합니다.
  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. IEWSClient 클래스를 사용하여 Exchange Server에 연결합니다.
  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);
    }
}