Manage Inbox Rules on Exchange Server in Java

While using Microsoft Exchange Server’s services, you may need to define different rules for the inbox folder. These rules are applied to the messages such as moving a message to a folder, deleting a message, etc. An inbox rule is comprised of conditions and the actions to be performed when the conditions are met. In this article, you will learn how to create and update inbox rules on MS Exchange Server in Java.

Java API to Manage Inbox Rules on Exchange Server

To work with inbox rules on MS Exchange Server, we will use Aspose.Email for Java. The API is designed to seamlessly work with MS Exchange Server and manage its services. You can either download the API or install it using the following Maven configurations.

Repository:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-email</artifactId>
    <version>22.3</version>
    <classifier>jdk16</classifier>
</dependency>

Create an Inbox Rule on Exchange Server in Java

Aspose.Email for Java uses Exchange Web Services (EWS) to work with inbox rules on Exchange Server. The following are the steps to create an inbox rule on Exchange Server in Java.

The following code sample shows how to create an inbox rule on Exchange Server in Java.

// Connect to Exchange Server
IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);
System.out.println("Connected to Exchange server");
// Create a new rule
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);
// Create rule
client.createInboxRule(rule);

Update an Inbox Rule on Exchange Server in Java

The following are the steps to fetch and update an existing inbox rule on Exchange Server in Java.

The following code sample shows how to update an inbox rule on MS Exchange Server in Java.

// Connect to Exchange Server
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) {
if ("Message from client ABC".equals(inboxRule.getDisplayName())) {
// Update rule
inboxRule.getConditions().getFromAddresses().set_Item(0, new MailAddress("administrator@ex2010.local", true));
client.updateInboxRule(inboxRule);
}
}

Get a Free API License

You can get a free temporary license to use Aspose.Email for Java without evaluation limitations.

Conclusion

In this article, you have learned how to work with inbox rules on Microsoft Exchange Server from within Java applications. You have seen how to add or update an inbox rule on Exchange Server in Java. In addition, you can explore the features of Aspose.Email for Java using the documentation. Also, in case you would have any questions, you can post to our forum.

See Also