/// <summary> /// Get events matching the subject. /// </summary> /// <returns> /// Array of relevant events or empty array. /// </returns> private ArrayList GetEventItems() { // Use a Jet Query to filter the details we need initially between the two specified dates string dateFilter = $"[Start] >= '{StartDate:g}' and [End] <= '{EndDate:g}'"; _Items calendarItems = s_mapiFolder.Items.Restrict(dateFilter); calendarItems.Sort("[Start]", Type.Missing); calendarItems.IncludeRecurrences = true; // Must use 'like' comparison for Find/FindNext string subjectFilter = !String.IsNullOrEmpty(Subject) ? $"@SQL=\"urn:schemas:httpmail:subject\" like '%{Subject.Replace("'", "''")}%'" : "@SQL=\"urn:schemas:httpmail:subject\" <> '!@#'"; // Use Find and FindNext methods to get all the items ArrayList resultArray = new ArrayList(); AppointmentItem eventItem = calendarItems.Find(subjectFilter) as AppointmentItem; while (eventItem != null) { resultArray.Add(eventItem); // Find the next event eventItem = calendarItems.FindNext() as AppointmentItem; } return(resultArray); }
public static MailItem GetEmail(string subject, string recipientEmail, DateTime maxAgeTimeStamp, int waitSeconds) { const int checkIntervalSeconds = 15; const string prSmtpAddress = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"; if (Process.GetProcessesByName("outlook").Length == 0) { Process.Start("outlook.exe"); } var item = new MailItem(); NetOffice.OutlookApi.MailItem oItem = null; var match = false; try { Application app = new Application(); _NameSpace ns = app.GetNamespace("MAPI"); ns.Logon(null, null, false, false); MAPIFolder inboxFolder = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox); _Items oItems = inboxFolder.Items; oItems.Sort("[ReceivedTime]", false); Console.WriteLine("DBG: Started looking for email at {0}", DateTime.Now); for (int j = 0; j <= waitSeconds; j = j + checkIntervalSeconds) { Thread.Sleep(TimeSpan.FromSeconds(checkIntervalSeconds)); for (int i = oItems.Count; i > 1; i--) { oItem = (NetOffice.OutlookApi.MailItem)oItems[i]; Console.WriteLine("DBG: Checking mail at {0} => found mail with timestamp: { 1}", DateTime.Now.ToLongTimeString(), oItem.SentOn.ToLongTimeString()); if ((oItem.ReceivedTime - maxAgeTimeStamp).TotalSeconds < 0) { break; } if (oItem.Subject.IsRegExMatch(subject) && oItem.Recipients.Single().PropertyAccessor.GetProperty(prSmtpAddress).ToString().Equals(recipientEmail)) { match = true; break; } //Console.WriteLine("Subject: {0}", item.Subject); //Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString()); } if (match) { break; } } } catch (System.Runtime.InteropServices.COMException) { throw new Exception("ERROR: retrieving the email failed due to System.Runtime.InteropServices.COMException"); } if (match) { item.EntryId = oItem.EntryID; item.Subject = oItem.Subject; item.SentDate = oItem.SentOn.ToLongDateString(); item.SentTime = oItem.SentOn.ToLongTimeString(); item.ReceivedDate = oItem.ReceivedTime.ToLongDateString(); item.ReceivedTime = oItem.ReceivedTime.ToLongTimeString(); item.Body = oItem.Body; item.HtmlBody = oItem.HTMLBody; item.HasMessage = true; item.PrintInfo(); } else { Console.WriteLine("DBG: Couldn't find message with subject matching {0} and recipient {1}", subject, recipientEmail); } Console.WriteLine("DBG: Finished looking for email at {0}", DateTime.Now); return(item); }