Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 14, 2022 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/654fcbd7a3342d45b4c993acc50ff036 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/654fcbd7a3342d45b4c993acc50ff036 to your computer and use it in GitHub Desktop.
Create and Update Inbox Rules 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);
// 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment