public void caseNumberReader() { var mails = OutlookEmails.ReadMailItems(); int i = 1; foreach (var mail in mails) { String pattern = @"[1]\d{14}"; String reader = mail.EmailSubject; Match match = Regex.Match(reader, pattern, RegexOptions.IgnoreCase); if (match.Success) { Console.WriteLine(match.Value); } } Console.ReadKey(); }
public static List <OutlookEmails> ReadMailItems() { Application outlookApplication = null; NameSpace outlookNamespace = null; MAPIFolder inboxFolder = null; Items mailItems = null; List <OutlookEmails> listEmailDetails = new List <OutlookEmails>(); OutlookEmails emailDetails; try { outlookApplication = new Application(); outlookNamespace = outlookApplication.GetNamespace("MAPI"); inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox); mailItems = inboxFolder.Items; foreach (dynamic item in mailItems) { if (item is MailItem) { emailDetails = new OutlookEmails(); emailDetails.EmailFrom = item.SenderEmailAddress; emailDetails.EmailSubject = item.Subject; emailDetails.EmailBody = item.Body; emailDetails.EmailTo = item.To; emailDetails.EmailDate = item.ReceivedTime; listEmailDetails.Add(emailDetails); ReleaseComObject(item); } } } catch (System.Exception ex) { Console.WriteLine(ex.Message); } finally { ReleaseComObject(mailItems); ReleaseComObject(inboxFolder); ReleaseComObject(outlookNamespace); ReleaseComObject(outlookApplication); } return(listEmailDetails); }
public static List <con> CaseNumberReader() { List <con> writeCon = new List <con>(); //List<string> id = new List<string>(); // List<DateTime> time = new List<DateTime>(); var mails = OutlookEmails.ReadMailItems(); foreach (var mail in mails) { String pattern1 = @"[1]\d{14}"; String CaseReader = mail.EmailSubject; Match match1 = Regex.Match(CaseReader, pattern1, RegexOptions.IgnoreCase); if (match1.Success) { con data = new con() { caseId = match1.Value, time = mail.EmailDate, alias = mail.EmailTo }; String pattern2 = @"Task"; Match match2 = Regex.Match(CaseReader, pattern2, RegexOptions.IgnoreCase); if (match2.Success) { data.isTask = "Collaboration Task"; } else { data.isTask = "Case"; } StringBuilder ser = new StringBuilder(); ser.Append(CaseReader); ser.Append(mail.EmailBody); string pattern3 = @"\s[A|B|C]\s"; Match match3 = Regex.Match(ser.ToString(), pattern3, RegexOptions.IgnoreCase); if (match3.Success) { data.severity = match3.Value; } else { data.severity = ""; } writeCon.Add(data); //id.Add(match1.Value); //time.Add(mail.EmailDate); } } //Sort the numbers from the oldest to the lastest. // id.Sort(); //remove the dulplicate numbers // List<string> id1=id.Distinct().ToList(); // time.ToString(); // Console.WriteLine(string.Join("\n", time)); // return id1; List <con> nonDuplicateList = new List <con>(); foreach (con mem in writeCon) { if (nonDuplicateList.Exists(x => x.caseId == mem.caseId) == false) { nonDuplicateList.Add(mem); } } var sortedData = (from s in nonDuplicateList select new { s.caseId, s.time, s.alias, s.severity }).Distinct().OrderBy(x => x.caseId).ToList(); foreach (var i in sortedData) { Console.WriteLine("caseId: " + i.caseId + " " + "SentTime: " + i.time + " " + "Alias: " + i.alias); } return(nonDuplicateList); }