public void GetMessages(Property[] inputs, RequiredProperties required, Property[] returns, MethodType methodType, ServiceObject serviceObject) { serviceObject.Properties.InitResultTable(); System.Data.DataRow dr; Helper h = new Helper(serviceBroker); string subjectfilter = string.Empty; string bodyfilter = string.Empty; string fromfilter = string.Empty; string startindex = string.Empty; string numberofmessages = string.Empty; string mailbox = inputs.Where(p => p.Name.Equals("mailbox")).FirstOrDefault().Value.ToString(); bool headersonly = bool.Parse(inputs.Where(p => p.Name.Equals("headersonly")).FirstOrDefault().Value.ToString()); bool setseen = bool.Parse(inputs.Where(p => p.Name.Equals("setseen")).FirstOrDefault().Value.ToString());; AE.Net.Mail.MailMessage mtemp = new AE.Net.Mail.MailMessage(); try { using (var ic = h.GetImapClient()) { AE.Net.Mail.MailMessage[] m = null; Lazy <AE.Net.Mail.MailMessage>[] mm = null; bool isLazy = false; switch (serviceObject.Methods[0].Name.ToLower()) { case "getallmessages": m = ic.GetMessages(0, ic.GetMessageCount(), headersonly, setseen); isLazy = false; break; case "getmessages": startindex = inputs.Where(p => p.Name.Equals("startindex")).FirstOrDefault().Value.ToString(); numberofmessages = inputs.Where(p => p.Name.Equals("numberofmessages")).FirstOrDefault().Value.ToString(); m = ic.GetMessages(int.Parse(startindex), int.Parse(numberofmessages), headersonly, setseen); isLazy = false; break; case "searchmessagesbysubject": subjectfilter = inputs.Where(p => p.Name.Equals("subjectfilter")).FirstOrDefault().Value.ToString(); mm = ic.SearchMessages(SearchCondition.Undeleted().And(SearchCondition.Subject(subjectfilter))); isLazy = true; break; case "searchmessagesbybody": bodyfilter = inputs.Where(p => p.Name.Equals("bodyfilter")).FirstOrDefault().Value.ToString(); mm = ic.SearchMessages(SearchCondition.Undeleted().And(SearchCondition.Body(bodyfilter))).ToArray(); isLazy = true; break; case "searchmessagesbyfrom": fromfilter = inputs.Where(p => p.Name.Equals("fromfilter")).FirstOrDefault().Value.ToString(); mm = ic.SearchMessages(SearchCondition.Undeleted().And(SearchCondition.From(fromfilter))).ToArray(); isLazy = true; break; } //AE.Net.Mail.MailMessage[] mm = ic.GetMessages(0, ic.GetMessageCount(), headersonly, setseen); if (isLazy) { foreach (System.Lazy <AE.Net.Mail.MailMessage> msg in mm) { AE.Net.Mail.MailMessage mmsg = msg.Value; dr = serviceBroker.ServicePackage.ResultTable.NewRow(); MapMailMessage(dr, mmsg); dr["mailbox"] = mailbox; dr["headersonly"] = headersonly; dr["setseen"] = setseen; switch (serviceObject.Methods[0].Name.ToLower()) { case "searchmessagesbysubject": dr["subjectfilter"] = subjectfilter; break; case "searchmessagesbybody": dr["bodyfilter"] = bodyfilter; break; case "searchmessagesbyfrom": dr["fromfilter"] = fromfilter; break; } dr["startindex"] = startindex; dr["numberofmessages"] = numberofmessages; serviceBroker.ServicePackage.ResultTable.Rows.Add(dr); } } else { foreach (AE.Net.Mail.MailMessage msg in m.OrderByDescending(p => p.Date)) { mtemp = msg; dr = serviceBroker.ServicePackage.ResultTable.NewRow(); MapMailMessage(dr, msg); dr["mailbox"] = mailbox; dr["headersonly"] = headersonly; dr["setseen"] = setseen; switch (serviceObject.Methods[0].Name.ToLower()) { case "searchmessagesbysubject": dr["subjectfilter"] = subjectfilter; break; case "searchmessagesbybody": dr["bodyfilter"] = bodyfilter; break; case "searchmessagesbyfrom": dr["fromfilter"] = fromfilter; break; } dr["startindex"] = startindex; dr["numberofmessages"] = numberofmessages; serviceBroker.ServicePackage.ResultTable.Rows.Add(dr); } } ic.Disconnect(); } } catch (Exception ex) { Console.WriteLine(mtemp.Subject); //serviceObject.Properties.BindPropertiesToResultTable(); } //serviceObject.Properties.BindPropertiesToResultTable(); }
public IEnumerable <MailMessage> GetMessages(string pattern) { string searchPattern = @"*" + pattern + @"*"; ImapClient client = new ImapClient(hostname, 993, Username, Password, AuthMethod.Login, true); IEnumerable <MailMessage> messages; using (client) { IEnumerable <uint> uids = client.Search(SearchCondition.From(searchPattern).Or(SearchCondition.Subject(searchPattern).Or(SearchCondition.Body(searchPattern)))); messages = client.GetMessages(uids.ToArray()); } return(messages); }