public List<Map<String, String>> readEmails(
      int count, WellKnownFolderName name, boolean doDelete) {
    System.out.println(
        "\n\n\n############### READING EMAILS for : " + name.name() + " #################");
    List<Map<String, String>> msgDataList = new ArrayList<>();
    try {
      Folder folder = Folder.bind(exchangeService, name);
      FindFoldersResults results =
          folder.findFolders(new SearchFilter.Exists(), new FolderView(10));

      FindFoldersResults result2 =
          folder.findFolders(
              new SearchFilter() {
                @Override
                protected String getXmlElementName() {
                  return "Other";
                }
              },
              new FolderView(10));

      // folder.delete(DeleteMode.HardDelete);
      FindItemsResults<Item> itemresults =
          exchangeService.findItems(folder.getId(), new ItemView(count));
      int i = 1;
      for (Item item : itemresults) {
        Map<String, String> messageData;
        messageData = readEmailItem(item.getId(), doDelete);
        if (!doDelete) {
          System.out.println("\nEmails #" + (i++) + ":");
          System.out.println("subject : " + messageData.get("subject"));
          System.out.println("Sender : " + messageData.get("senderName").toString());
          msgDataList.add(messageData);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return msgDataList;
  }
 /**
  * Reading one email at a time. Using Item ID of the email. Creating a message data map as a
  * return value.
  */
 public Map readEmailItem(ItemId itemId, boolean doDelete) {
   Map messageData = new HashMap();
   try {
     Item itm = Item.bind(exchangeService, itemId, PropertySet.FirstClassProperties);
     EmailMessage emailMessage = EmailMessage.bind(exchangeService, itm.getId());
     if (doDelete) {
       System.out.println("deleting item: " + itm.getSubject());
       itm.delete(DeleteMode.HardDelete);
       emailMessage.delete(DeleteMode.HardDelete);
     } else {
       messageData.put("emailItemId", emailMessage.getId().toString());
       messageData.put("subject", emailMessage.getSubject());
       messageData.put("fromAddress", emailMessage.getFrom().getAddress().toString());
       messageData.put("senderName", emailMessage.getSender().getName().toString());
       Date dateTimeCreated = emailMessage.getDateTimeCreated();
       messageData.put("SendDate", dateTimeCreated.toString());
       Date dateTimeReceived = emailMessage.getDateTimeReceived();
       messageData.put("ReceivedDate", dateTimeReceived.toString());
       messageData.put("Size", emailMessage.getSize() + "");
       messageData.put("emailBody", emailMessage.getBody().toString());
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
   return messageData;
 }
  public void findFolder() throws Exception {
    // Create a view with a page size of 10.
    FolderView view = new FolderView(10);

    // Identify the properties to return in the results set.
    view.setPropertySet(new PropertySet(IdOnly));
    view.getPropertySet().add(FolderSchema.DisplayName);

    // Return only folders that contain items.
    SearchFilter searchFilter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 100);

    // Unlike FindItem searches, folder searches can be deep traversals.
    view.setTraversal(FolderTraversal.Deep);

    // Send the request to search the mailbox and get the results.
    FindFoldersResults findFolderResults =
        exchangeService.findFolders(WellKnownFolderName.Root, searchFilter, view);

    // Process each item.
    for (Folder myFolder : findFolderResults.getFolders()) {
      System.out.println(myFolder.getDisplayName());
      if (myFolder.getDisplayName().equalsIgnoreCase("Other")) {
        FindItemsResults<Item> itemresults;
        int count = 0;
        do {
          // empty the deleted folder
          // emptyDelete();
          // move items from other to deleted items
          itemresults = exchangeService.findItems(myFolder.getId(), new ItemView(100));
          System.out.println(" Is more Avail: " + itemresults.isMoreAvailable());
          for (Item item : itemresults.getItems()) {
            count++;
            System.out.println(item.getSubject());
            // Item itm = Item.bind(exchangeService, itemId, PropertySet.FirstClassProperties);
            EmailMessage emailMessage = EmailMessage.bind(exchangeService, item.getId());
            System.out.println("deleting : " + emailMessage.getSubject());
            try {
              emailMessage.delete(DeleteMode.HardDelete);
              // item.delete(DeleteMode.HardDelete);
              // item.move(WellKnownFolderName.DeletedItems);
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        } while (itemresults.isMoreAvailable());
        System.out.println("Item deleted: " + count);
        // emptyDelete();
      }
    }
  }