static void Main() { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //connect to exchange //autodiscoverurl //Service service = new Service(); Program prog = new Program(); ExchangeService service = prog.getMService(); // Bind the Inbox folder to the service object. Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, WellKnownFolderName.Inbox); // The search filter to get unread email. Console.WriteLine("BEFORE"); //TIP to create search filter //first create search collection list //add any search filter like a filter that looks for a certain email or checks if email is unread or read //then if you want more searches, create a new search filter that ANDS the previous list collection //then if you want to let's say check for a domain name in the email, create new list //with that new list, add new search filter & add previous filter //then create a new search filter and AND or OR it with previous collection //idea is this starts chaining filters together //Another tip: Make sure you properly use AND and OR...e.g. can't filter two email domains // and say you want the substring to contain denali AND tmobile // so you choose OR operator to say i want to filter for either domain //SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false)); List <SearchFilter> searchANDFilter = new List <SearchFilter>(); //searchANDFilter.Add(sf); ExtendedPropertyDefinition PidTagSenderSmtpAddress = new ExtendedPropertyDefinition(0x5D01, MapiPropertyType.String); //searchANDFilter.Add(new SearchFilter.ContainsSubstring(PidTagSenderSmtpAddress, "@denaliai.com")); searchANDFilter.Add(new SearchFilter.ContainsSubstring(PidTagSenderSmtpAddress, "@denaliai.com")); searchANDFilter.Add(new SearchFilter.ContainsSubstring(PidTagSenderSmtpAddress, "@usc.edu")); //Console.WriteLine("the address is: " + PidTagSenderSmtpAddress.PropertySet.Value.ToString()); SearchFilter domainSF = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchANDFilter); List <SearchFilter> searchFinalFilter = new List <SearchFilter>(); searchFinalFilter.Add(new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false))); searchFinalFilter.Add(domainSF); SearchFilter finalSF = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFinalFilter); //new SearchFilter.ContainsSubstring(EmailMessageSchema.Sender, "@denaliai.com", ContainmentMode.Substring, ComparisonMode.IgnoreCase) //SearchFilter.ContainsSubstring subjectFilter = new SearchFilter.ContainsSubstring(EmailMessageSchema.Sender,"@denaliai.com", ContainmentMode.Substring, ComparisonMode.IgnoreCase); Console.WriteLine("AFTER"); ItemView view = new ItemView(4); // Fire the query for the unread items. // This method call results in a FindItem call to EWS. view.PropertySet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.Sender); view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending); //Queue<FindItemsResults<Item>> allEmails = new Queue<FindItemsResults<Item>>(); //Queue<EmailMessage> allEmails = new Queue<EmailMessage>(); FindItemsResults <Item> findResults = service.FindItems(WellKnownFolderName.Inbox, finalSF, view); try { //allEmails.Enqueue(message); MailItem mail = new MailItem(); Stack <Item> allEmails = new Stack <Item>(); foreach (Item item in findResults.Items) { allEmails.Push(item); } foreach (Item item in allEmails) { mail.loadMail(findResults, item, service, prog); prog.setMailItem(mail); prog.setMProjectTitle(mail.mSubject); Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&the project title is: " + prog.getMProjectTitle()); if (mail.mIsAuthority == true) { //idea is to send project report if authority is true //EmailErrors ee = new EmailErrors(prog); //ee.sendMilestoneErrorEmail(); try { Sharepoint sp = new Sharepoint(prog); string fullReport = sp.createProjectReport(prog.getMProjectTitle()); //Create Response Email mail.sendEmail(fullReport, prog.getMProjectTitle(), service); } catch (ServerException se) { if (se.Message.Contains("does not exist at site with URL")) { EmailErrors ee = new EmailErrors(prog); ee.sendProjectDoesNotExistEmail(mail.mMailboxAddress); } } } else { HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); //doc.LoadHtml(body); doc.LoadHtml(mail.mBody); String fullBodyText = ""; foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//text()")) { if (node.InnerText != "") { Console.WriteLine(node.InnerText); fullBodyText += "\n" + node.InnerText; } } //fullBodyText = adjustString(fullBodyText); prog.setMBody(fullBodyText); //prog.setMProjectTitle(subject); prog.setMProjectTitle(mail.mSubject); Console.WriteLine("body is: " + prog.getMBody()); prog.parseEmail(prog, service); } } } catch (Exception e) { Console.WriteLine(e); } } catch (Exception e) { Console.WriteLine(e); } }
public void setMailItem(MailItem value) { this.mMailItem = value; }