/// <summary> /// Get a list of all the unprocess email ids /// We don't return the emails as anotehr thread could 'change' those values. /// </summary> /// <param name="folder">The folder we are working in</param> /// <param name="limit"></param> /// <param name="includeSubfolders"></param> /// <param name="restrictFolder">the filter we want to use to look in the folder.</param> /// <param name="token"></param> /// <returns></returns> private IList <string> GetUnprocessedEmailsInFolder( Outlook.MAPIFolder folder, int limit, bool includeSubfolders, string restrictFolder, CancellationToken token) { // do the sub folders. var ids = new List <string>(); if (includeSubfolders) { ids.AddRange(GetUnprocessedEmailsInFolders(folder.Folders, limit, true, restrictFolder, token)); } // is it a mail folder? if (folder.DefaultItemType != Outlook.OlItemType.olMailItem) { return(ids); } var restrictedItems = string.IsNullOrWhiteSpace(restrictFolder) ? folder.Items : folder.Items.Restrict(restrictFolder); // add an more items to the progress bar _progress.AddRange(restrictedItems.Count, limit); foreach (var item in restrictedItems) { if (token.IsCancellationRequested) { return(new List <string>()); } // step forward. _progress.Step(); // get the mail item if (!(item is Outlook._MailItem mailItem)) { continue; } try { // add this to the mail processor... ids.Add(mailItem.EntryID); _logger.LogInformation($"Found unprocessed email...{mailItem.Subject}."); if (ids.Count >= limit) { break; } } catch (Exception e) { _logger.LogError("There was an exception looking at unprocessed folder"); _logger.LogException(e); } } return(ids); }