void AddToList(Email email) { try { if (email == null) { return; } if (!EmailsFound.Contains(email, _emailCompare)) { EmailsFound.Add(email); } } catch (System.Exception ex) { OnSearchErrorOccurred?.Invoke(ex, EventArgs.Empty); Logger.Log($"Failed to return Outlook accounts\n Error: {ex.Message}", "Error"); } }
bool Compare() { return(EmailsFound.SequenceEqual(PreviousListFound, _emailCompare)); }
/// <summary> /// used to asynchronously add files to EmailsFoundList /// </summary> void SearchInBox() { var watch = new Stopwatch(); watch.Start(); Logger.Log("Starting search for Email Account: " + EmailAddress); // Duplicate list for comparing equity PreviousListFound = EmailsFound; EmailsFound.Clear(); // create item for later disposing of com objects Outlook.MAPIFolder sentBox = null; Outlook.Items olItems = null; Outlook.MailItem mailItem = null; Outlook.MAPIFolder account = null; try { _olApp = new OutlookApp(); account = GetAccountExplicit(); if (account == null) { throw new ApplicationException("Failed to extract email account"); } sentBox = account.Store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail); olItems = sentBox.Items; // order items in Email DB by date sent olItems.Sort("SentOn", true); double Counter = 0; _hasError = false; foreach (var item in olItems) { mailItem = item as Outlook.MailItem; HasError = false; if (mailItem != null) { string body = mailItem.Body; if (!string.IsNullOrEmpty(body) && body.Contains(this.SearchPhrase)) { AddToList(new Email(mailItem)); } } Counter++; percentComplete = Math.Round(Counter / SearchSize * 100); if (Counter >= SearchSize) { break; } if (EmailsFound.Count >= MaxDisplayItems) { break; } } } catch (System.Exception ex) { Logger.Log($"Error when running email search for account: {EmailAddress}\n Error: {ex.Message}", "Error"); OnSearchErrorOccurred?.Invoke(ex, EventArgs.Empty); _firstRun = false; } finally { if (sentBox != null) { Marshal.ReleaseComObject(sentBox); } if (olItems != null) { Marshal.ReleaseComObject(olItems); } if (mailItem != null) { Marshal.ReleaseComObject(mailItem); } if (account != null) { Marshal.ReleaseComObject(account); } if (_olApp != null) { Marshal.ReleaseComObject(_olApp); } _isSearchRunning = false; _firstRun = false; OnSearchComplete?.Invoke(this, EventArgs.Empty); Logger.Log("New Emails Detected, raising OnSearchComplete event"); } watch.Stop(); Logger.Log("Search Took " + watch.ElapsedMilliseconds); Logger.Log("Email Account Search: " + this.EmailAddress + " Emails Found: " + EmailsFound.Count); }