0

I have a working code that connects to Microsoft outlook and fetches Mail based on some filters. It is working fine with reading the direct mail but not able to pick the same mail if forwarded. Any Help is appreciated.

    List<SearchFilter> searchFilterCollection = new ArrayList<>();
    searchFilterCollection.add(new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived,localDate));

    // flag to pick only email which contains attachments
    searchFilterCollection.add(new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, Boolean.TRUE));
    List<MetaInfoDTO> filterList = channel.getFilters();
    // for each channel
    log.info("Email from: {}", definedChannelFilter.getFieldData());
    EmailAddress manager = new EmailAddress("abcd@outlook.com");
    SearchFilter.IsEqualTo fromManagerFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, manager);
    searchFilterCollection.add(fromManagerFilter);
    log.info("Email Subject: {}", definedChannelFilter.getFieldData());
    searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Subject,"Subject ASDF"));
    log.info("Email Body Content: {}", definedChannelFilter.getFieldData());
    searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Body,"Body Content if any"));

    return new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);        
Florian
  • 2,796
  • 1
  • 15
  • 25
Abhishek kumar
  • 148
  • 1
  • 2
  • I believe you are reading using ews-java-api , can you mention version of ews-java-api and edit code to [mwe](https://stackoverflow.com/help/minimal-reproducible-example) – Hemant Apr 13 '20 at 02:23

1 Answers1

1

You must revisit filters in your code.

Here is my code to read attached emails and I am able to print forwarded emails with attachments.

private static void readAttachmentEmail(ExchangeService service) throws Exception {
        // Bind to the Inbox.
        Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
        ItemView view = new ItemView(5);
        List<SearchFilter> searchFilterCollection = new ArrayList<>();
        // flag to pick only email which contains attachments
        searchFilterCollection.add(new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, Boolean.TRUE));
        // for each channel
        EmailAddress manager = new EmailAddress("manager@email.com");
        SearchFilter.IsEqualTo fromManagerFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, manager);
        searchFilterCollection.add(fromManagerFilter);
        //searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Subject,"Subject ASDF"));
        //searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Body,"Body Content if any"));
        SearchFilter finalSearchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);
        service.findItems(inbox.getId(), finalSearchFilter, view).forEach(item->{
            try {
                System.out.println("id==========" + item.getId());
                System.out.println("sub==========" + item.getSubject());
            } catch (ServiceLocalException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });
    }
Hemant
  • 1,403
  • 2
  • 11
  • 21