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;
  }
Пример #2
0
 public ExchangeProvider(String fromAddress, String fromName, String password) {
   ExchangeService service = new ExchangeService();
   ExchangeCredentials credentials = new WebCredentials(fromAddress, password);
   service.setCredentials(credentials);
   try {
     service.autodiscoverUrl(fromAddress, new ExchangeRedirection());
   } catch (Exception e) {
     System.out.println("Exception from the autodiscoverUrl method call. Exiting.");
     System.exit(1);
   }
   this.service = service;
 }
  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();
      }
    }
  }
 public void login(Properties properties) throws Exception {
   String exchangeEmail = properties.getProperty("exchange.email");
   String exchangePassword = properties.getProperty("exchange.password");
   // String exchangeUrl = properties.getProperty("exchange.url");
   Assert.notNull(exchangeEmail, "Exchange email is null. Please provide a valid email.");
   Assert.notNull(exchangePassword, "Exchange password is null. Please provide a valid password.");
   // Assert.notNull(exchangeUrl, "Exchange url is null. Please provide a valid url to connect.");
   exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
   // exchangeService = new ExchangeService();
   ExchangeCredentials credentials = new WebCredentials(exchangeEmail, exchangePassword);
   exchangeService.setCredentials(credentials);
   // service.setUrl(URI.create(exchangeUrl));
   exchangeService.autodiscoverUrl(exchangeEmail, new RedirectionUrlCallback());
   isLoggedIn = true;
 }