1

How can I specify the SearchFilter with Java EWS library to fetch mails containing defined subject line?

Thanks in advance.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Anant Agarwal
  • 63
  • 1
  • 10

1 Answers1

5

Assuming you mean the library created by Microsoft, here is an example directly from the Getting started with EWS Java API.rtf included in the download:

public void findItems()
{
ItemView view = new ItemView(10);
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
view.setPropertySet(new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject,
        ItemSchema.DateTimeReceived));


FindItemsResults<Item> findResults = service.findItems(WellKnownFolderName.Inbox, new SearchFilter.SearchFilterCollection(
LogicalOperator.Or, new SearchFilter.ContainsSubstring(ItemSchema.Subject, "EWS"), 
new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API")),view);

System.out.println("Total number of items found: " + findResults.getTotalCount());

for (Item item : findResults)
    {
            System.out.println(item.getSubject());
            System.out.println(item.getBody());
            // Do something with the item.
    }
}
Michael Mainer
  • 3,387
  • 1
  • 13
  • 32
  • This is working fine .. Thanks for help. Though I am trying for not having case sensitive . If you are aware pls let me know. – Anant Agarwal Feb 27 '14 at 07:21
  • Hello, I have problems to sort the items when use the findItems() method. The way that you use in the previous answer works? – Marin Apr 12 '16 at 17:22
  • 1
    If in case the subject line has special characters like ΓΏ etc, the find items won't retrieve anything. – Lucky Apr 14 '17 at 16:21