在 Exchange Server 上使用规则
Contents
[
Hide
]
管理规则
Aspose.Email for Java 可用于使用以下方式管理 Exchange Server 上的规则: EWSClient 类。此类使用 Exchange Web Services (EWS),该服务在 Exchange Server 2007 及以后版本中可用。为演示如何管理规则,本文解释了如何:
- 读取服务器上已有的规则。
- 创建新规则。
- 更新现有规则。
本文描述的所有功能均需要 Microsoft Exchange Server 2010 Service Pack 1。
读取规则
从 Exchange Server 获取所有规则:
- 使用 IEWSClient 类连接到 Exchange Server。
- 调用 IEWSClient.getInboxRules() 方法获取所有规则。
- 在 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 上创建新规则,请执行以下步骤:
- 使用 IEWSClient 类连接到 Exchange Server。
- 创建 InboxRule 类的新实例并设置以下必填属性:
- 显示名称
- 条件
- 操作
- 调用 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 上更新规则:
- 使用 IEWSClient 类连接到 Exchange Server。
- 调用 IEWSClient.getInboxRules() 方法获取所有规则。
- 在 foreach 循环中,遍历所有规则,并通过匹配条件中的 DisplayName 来获取要更改的规则。
- 更新规则属性
- 调用 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);
}
}