public static void GetAllSubFolders(ExchangeService service, ExchangeFolder currentFolder, List <ExchangeFolder> folderStore, bool skipEmpty = true)
        {
            Logger.Debug("Looking for sub folders of '" + currentFolder.FolderPath + "'");
            var results = service.FindFolders(currentFolder.Folder.Id, new FolderView(int.MaxValue));

            foreach (var folder in results)
            {
                String folderPath = (String.IsNullOrEmpty(currentFolder.FolderPath)
                    ? ""
                    : currentFolder.FolderPath + @"\") + folder.DisplayName;
                if (currentFolder.IsPublicFolder)
                {
                    folderPath = Regex.Replace(folderPath, @"^Global Public Folder Root\\", @"");
                }
                if (skipEmpty && folder.TotalCount == 0 && folder.ChildFolderCount == 0)
                {
                    Logger.Debug("Skipping folder " + folderPath + ", no messages, no subfolders.");
                    continue;
                }
                Logger.Debug("Found folder " + folderPath + ", " + folder.TotalCount + " messages in total.");
                var exchangeFolder = new ExchangeFolder()
                {
                    Folder         = folder,
                    FolderPath     = folderPath,
                    MessageCount   = folder.TotalCount,
                    FolderId       = folder.Id,
                    IsPublicFolder = currentFolder.IsPublicFolder
                };
                // only add it to the list of folders if it isn't the public folder root
                if (!currentFolder.IsPublicFolder || !String.Equals(folder.DisplayName, "Global Public Folder Root"))
                {
                    folderStore.Add(exchangeFolder);
                }
                if (exchangeFolder.Folder.ChildFolderCount > 0)
                {
                    GetAllSubFolders(service, exchangeFolder, folderStore, skipEmpty);
                }
            }
        }
Beispiel #2
0
        private bool FindExchangeMessages(ExchangeFolder exchangeFolder, SearchFilter.SearchFilterCollection filter, ItemView view, List <EmailMessage> emails, PropertySet fullPropertySet)
        {
            int majorFailureCount = 0;
            int tryCount          = 0;

            do
            {
                emails.Clear();
                tryCount++;
                var start = Environment.TickCount;
                try
                {
                    Logger.Debug("Retrieving next email messages from " + exchangeFolder.FolderPath);
                    // Get results
                    FindItemsResults <Item> findResults = exchangeFolder.Folder.FindItems(filter, view);
                    // Filter out non mail items
                    foreach (var item in findResults)
                    {
                        if (item is EmailMessage)
                        {
                            try
                            {
                                emails.Add((EmailMessage)item);
                                if (TestOnly && emails.Count > 20)
                                {
                                    break;
                                }
                            }
                            catch (Exception e)
                            {
                                FailedMessageCount++;
                                Logger.Error("Failed to extract email message from results, skipping : " + e.Message, e);
                            }
                        }
                        else
                        {
                            Logger.Warn("Will not export message, item is not a mail message [" + item.GetType() + "]");
                            IgnoredMessageCount++;
                        }
                    }
                    if (emails.Count > 0)
                    {
                        // Load properties for our final list of mails
                        Logger.Debug("Retrieving " + emails.Count + " emails properties from " +
                                     exchangeFolder.FolderPath);
                        service.LoadPropertiesForItems(emails, fullPropertySet);
                    }
                    Logger.Debug("Retrieved " + emails.Count + " messages from " + exchangeFolder.FolderPath + " in " + (Environment.TickCount - start) + "ms, trying " + tryCount + " times. " + (findResults.MoreAvailable ? "More" : "No more") + " messages in folder available.");
                    return(findResults.MoreAvailable && !TestOnly);
                }
                catch (ServerBusyException e)
                {
                    Logger.Warn(
                        "Received Server Busy Response (ServerBusyException), will back off and try again after (30s) : " +
                        e.Message);
                    Thread.Sleep(30000);
                }
                catch (XmlException e)
                {
                    Logger.Error("XML Response invalid (XmlException), will attempt again : " +
                                 e.Message);
                    Thread.Sleep(1000);
                }
                catch (TimeoutException e)
                {
                    Logger.Error("Request timed out with TimeoutException, will back off and try again after (30s) : " +
                                 e.Message);
                    Thread.Sleep(30000);
                }
                catch (ServiceRequestException e)
                {
                    Logger.Error(
                        "Service request failed with ServiceRequestException, this may not be a permanent error, will back off and try again after (30s) : " +
                        e.Message);

                    Thread.Sleep(30000);
                } catch (Exception e)
                {
                    majorFailureCount++;
                    if (majorFailureCount >= 10)
                    {
                        throw e;
                    }
                    else
                    {
                        Logger.Error("Failed to get emails/properties, will attempt " + (10 - majorFailureCount) + " more times after 30s pause.", e);
                        Thread.Sleep(30000);
                    }
                }
            } while(true);
        }