/// <summary>
        /// Quickly create a search query specifying the content string to search for
        /// </summary>
        /// <param name="content">The text to search with the Text and HTML content sections</param>
        /// <returns>IMAPSearchQuery object suitable for passing into the search methods</returns>
        public static IMAPSearchQuery QuickSearchContent(string content)
        {
            IMAPSearchQuery query = new IMAPSearchQuery();

            query.Content = content;

            return(query);
        }
        /// <summary>
        /// Quickly create a search query searching only for new messages
        /// </summary>
        /// <returns>IMAPSearchQuery object suitable for passing into the search methods</returns>
        public static IMAPSearchQuery QuickSearchNew()
        {
            IMAPSearchQuery query = new IMAPSearchQuery();

            query.New = true;

            return(query);
        }
        /// <summary>
        /// Quickly create a search query specifying the subject string to search for
        /// </summary>
        /// <param name="subject">The text to search for within the subject</param>
        /// <returns>IMAPSearchQuery object suitable for passing into the search methods</returns>
        public static IMAPSearchQuery QuickSearchSubject(string subject)
        {
            IMAPSearchQuery query = new IMAPSearchQuery();

            query.Subject = subject;

            return(query);
        }
        /// <summary>
        /// Quickly create a search query searching only for messages who date is within the date range
        /// </summary>
        /// <param name="sdate"></param>
        /// <param name="edate"></param>
        /// <returns></returns>
        public static IMAPSearchQuery QuickSearchDateRange(DateTime sdate, DateTime edate)
        {
            IMAPSearchQuery query = new IMAPSearchQuery();

            query.Range = new DateRange(sdate, edate);

            return(query);
        }
        /// <summary>
        /// Quickly create a search query specifying the display name and/or email address for the BCC: field
        /// </summary>
        /// <param name="name">display name of the person to search for</param>
        /// <param name="addr">e-mail address of the person to search for</param>
        /// <returns>IMAPSearchQuery object suitable for passing into the search method</returns>
        public static IMAPSearchQuery QuickSearchBCC(string name, string addr)
        {
            IMAPSearchQuery query = new IMAPSearchQuery();
            IMAPMailAddress a     = new IMAPMailAddress();

            a.Address     = addr;
            a.DisplayName = name;
            query.BCC.Add(a);

            return(query);
        }
        public override IEnumerable<Mail> GetMails(string subject, bool getInbox, bool getSent)
        {
            IMAPSearchQuery sq = new IMAPSearchQuery();
            sq.Subject = subject;

            // InterIMAP remembers only lates result, so will use only 1 result and update it again later
            IMAPSearchResult result = null;
            IMAPMessageCollection messages = null;

            if (getInbox)
            {
                IMAPFolder inbox = _client.GetSingleFolder(_inboxName);

                if (!string.IsNullOrEmpty(subject))
                {
                    result = inbox.Search(sq);
                    messages = result.Messages;
                }
                else
                    messages = inbox.Messages;

                foreach (IMAPMessage m in messages)
                    yield return convertToMail(m);

            }

            if (getSent)
            {
                IMAPFolder sentFolder = _client.GetSingleFolder(_sentFolderName);
                IMAPFolder sentSubFolder = sentFolder.SubFolders[_sentSubFolderName];

                if (!string.IsNullOrEmpty(subject))
                {
                    result = sentSubFolder.Search(sq);
                    messages = result.Messages;
                }
                else
                    messages = sentSubFolder.Messages;

                foreach (IMAPMessage m in result.Messages)
                    yield return convertToMail(m);
            }
        }
示例#7
0
        /// <summary>
        /// Searches this folder, and optionally the sub-folders for the specified query
        /// </summary>
        /// <param name="query">IMAPSearchQuery object containing the search options</param>
        /// <returns>A new IMAPSearchResult object containing the results of the query</returns>
        public IMAPSearchResult Search(IMAPSearchQuery query)
        {
            if (_client.OfflineMode)
            {
                return(OfflineSearch(query));
            }

            IMAPSearchResult result = new IMAPSearchResult();

            result.Query  = query;
            result.Folder = this;
            string        cmd         = "UID SEARCH {0}\r\n";
            StringBuilder q           = new StringBuilder();
            ArrayList     searchTerms = new ArrayList();

            // first we need to analyze the query and build our search command
            if (query.To.Count > 0)
            {
                foreach (IMAPMailAddress a in query.To)
                {
                    if (a.DisplayName != String.Empty)
                    {
                        searchTerms.Add(String.Format("TO \"{0}\"", a.DisplayName));
                    }

                    if (a.Address != String.Empty)
                    {
                        searchTerms.Add(String.Format("TO \"{0}\"", a.Address));
                    }
                }
            }

            if (query.From.Count > 0)
            {
                foreach (IMAPMailAddress a in query.From)
                {
                    if (a.DisplayName != String.Empty)
                    {
                        searchTerms.Add(String.Format("FROM \"{0}\"", a.DisplayName));
                    }

                    if (a.Address != String.Empty)
                    {
                        searchTerms.Add(String.Format("FROM \"{0}\"", a.Address));
                    }
                }
            }

            if (query.CC.Count > 0)
            {
                foreach (IMAPMailAddress a in query.CC)
                {
                    if (a.DisplayName != String.Empty)
                    {
                        searchTerms.Add(String.Format("CC \"{0}\"", a.DisplayName));
                    }

                    if (a.Address != String.Empty)
                    {
                        searchTerms.Add(String.Format("CC \"{0}\"", a.Address));
                    }
                }
            }

            if (query.BCC.Count > 0)
            {
                foreach (IMAPMailAddress a in query.BCC)
                {
                    if (a.DisplayName != String.Empty)
                    {
                        searchTerms.Add(String.Format("BCC \"{0}\"", a.DisplayName));
                    }

                    if (a.Address != String.Empty)
                    {
                        searchTerms.Add(String.Format("BCC \"{0}\"", a.Address));
                    }
                }
            }

            if (query.Subject != String.Empty)
            {
                searchTerms.Add(String.Format("SUBJECT \"{0}\"", query.Subject));
            }

            if (query.Content != String.Empty)
            {
                searchTerms.Add(String.Format("BODY {0}", query.Content));
            }

            if (query.Date != String.Empty)
            {
                searchTerms.Add(String.Format("ON {0}", FormatDateToServer(query.Date)));
            }

            if (query.BeforeDate != String.Empty)
            {
                searchTerms.Add(String.Format("SENTBEFORE {0}", FormatDateToServer(query.BeforeDate)));
            }

            if (query.AfterDate != String.Empty)
            {
                searchTerms.Add(String.Format("SENTSINCE {0}", FormatDateToServer(query.AfterDate)));
            }

            if (query.Range != null)
            {
                searchTerms.Add(String.Format("SENTBEFORE {0}", FormatDateToServer(query.Range.EndDate)));
                searchTerms.Add(String.Format("SENTSINCE {0}", FormatDateToServer(query.Range.StartDate)));
                searchTerms.Add(String.Format("SENTON {0}", FormatDateToServer(query.Range.StartDate)));
                searchTerms.Add(String.Format("SENTON {0}", FormatDateToServer(query.Range.EndDate)));
            }

            if (query.Answered)
            {
                searchTerms.Add("ANSWERED");
            }

            if (query.Deleted)
            {
                searchTerms.Add("DELETED");
            }

            if (query.Draft)
            {
                searchTerms.Add("DRAFT");
            }

            if (query.New)
            {
                searchTerms.Add("NEW");
            }

            if (query.Recent)
            {
                searchTerms.Add("RECENT");
            }

            if (query.LargerThan > -1)
            {
                searchTerms.Add(String.Format("LARGER {0}", query.LargerThan));
            }

            if (query.SmallerThan > -1)
            {
                searchTerms.Add(String.Format("SMALLER {0}", query.SmallerThan));
            }

            foreach (string s in searchTerms)
            {
                q.Append(s);
                q.Append(" ");
            }

            if (!this.IsCurrentlyExamined || !this.IsCurrentlySelected)
            {
                this.Examine();
            }

            ArrayList cmdResult = new ArrayList();

            cmd = String.Format(cmd, q.ToString().Trim());
            if (searchTerms.Count == 0)
            {
                throw new ArgumentNullException("No search terms were found");
            }

            _client._imap.SearchMessage(new string[] { q.ToString().Trim() }, true, cmdResult);

            foreach (string id in cmdResult)
            {
                if (id == String.Empty)
                {
                    continue;
                }

                foreach (IMAPMessage msg in _messages)
                {
                    if (msg.Uid == Convert.ToInt32(id))
                    {
                        result.Messages.Add(msg);
                    }
                }
            }



            return(result);
        }
        private Dictionary<StatType, int[]> getDateStatistics(IMAPFolder folder, List<StatType> statTypes, string subject)
        {
            IMAPSearchQuery sq = new IMAPSearchQuery();
            sq.Subject = subject;
            IMAPSearchResult result = folder.Search(sq);

            Dictionary<StatType, int[]> retVal = base.GetDateStatistics<IMAPMessage>(statTypes, result.Messages, "Date");

            return retVal;
        }
        private Dictionary<string, int> getUserStatistics(IMAPFolder folder, string subject)
        {
            IMAPSearchQuery sq = new IMAPSearchQuery();
            sq.Subject = subject;
            IMAPSearchResult result = folder.Search(sq);

            Dictionary<string, int> retVal = base.GetUserSatistics<IMAPMessage, IMAPMailAddress>(result.Messages, "From", "Address");

            return retVal;
        }
        public override int GetMessageCountBySubject(string subject, bool getInbox, bool getSent)
        {
            if (string.IsNullOrEmpty(subject))
                return GetMessageCount(getInbox, getSent);

            IMAPSearchQuery sq = new IMAPSearchQuery();
            sq.Subject = subject;
            IMAPSearchResult result = null;

            int retVal = 0;

            if (getInbox)
            {
                IMAPFolder inbox = _client.GetSingleFolder(_inboxName);
                result = inbox.Search(sq);
                retVal += result.Messages.Count;
            }

            if (getSent)
            {
                IMAPFolder SentFolder = _client.GetSingleFolder(_sentFolderName);
                IMAPFolder SentSubFolder = SentFolder.SubFolders[_sentSubFolderName];
                result = SentSubFolder.Search(sq);
                retVal += result.Messages.Count;
            }

            return retVal;
        }
        private List<Mail> convertMessagesToCollection(string subject, IMAPFolder folder)
        {
            IMAPSearchQuery sq = new IMAPSearchQuery();
            sq.Subject = subject;
            IMAPSearchResult result = folder.Search(sq);

            List<Mail> retVal = new List<Mail>();

            foreach (IMAPMessage m in result.Messages)
            {
                retVal.Add(convertToMail(m));
            }

            return retVal;
        }
示例#12
0
 /// <summary>
 /// Default Constructor
 /// </summary>
 public IMAPSearchResult()
 {
     _query = null;
     _folder = null;
     _messages = new IMAPMessageCollection();
 }
 /// <summary>
 /// Default Constructor
 /// </summary>
 public IMAPSearchResult()
 {
     _query    = null;
     _folder   = null;
     _messages = new IMAPMessageCollection();
 }
示例#14
0
        /// <summary>
        /// Quickly create a search query searching only for messages who date is within the date range
        /// </summary>
        /// <param name="sdate"></param>
        /// <param name="edate"></param>
        /// <returns></returns>
        public static IMAPSearchQuery QuickSearchDateRange(string sdate, string edate)
        {
            IMAPSearchQuery query = new IMAPSearchQuery();
            query.Range = new DateRange(sdate, edate);

            return query;
        }
示例#15
0
        /// <summary>
        /// Quickly create a search query specifying the content string to search for
        /// </summary>
        /// <param name="content">The text to search with the Text and HTML content sections</param>
        /// <returns>IMAPSearchQuery object suitable for passing into the search methods</returns>
        public static IMAPSearchQuery QuickSearchContent(string content)
        {
            IMAPSearchQuery query = new IMAPSearchQuery();
            query.Content = content;

            return query;
        }
示例#16
0
        /// <summary>
        /// Private method for manually searching the cache when in offline mode
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        private IMAPSearchResult OfflineSearch(IMAPSearchQuery query)
        {
            IMAPSearchResult result = new IMAPSearchResult();

            result.Query = query;

            foreach (IMAPMessage msg in _messages)
            {
                if (msg == null)
                {
                    continue;
                }

                #region To
                if (query.To.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.To)
                    {
                        if (!AddressListContains(msg.To, a))
                        {
                            containsAll = false;
                            break;
                        }
                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region From
                if (query.From.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.From)
                    {
                        if (!AddressListContains(msg.From, a))
                        {
                            containsAll = false;
                            break;
                        }
                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region CC
                if (query.CC.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.CC)
                    {
                        if (!AddressListContains(msg.Cc, a))
                        {
                            containsAll = false;
                            break;
                        }
                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region BCC
                if (query.BCC.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.BCC)
                    {
                        if (!AddressListContains(msg.Bcc, a))
                        {
                            containsAll = false;
                            break;
                        }
                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Subject
                if (query.Subject != String.Empty)
                {
                    if (msg.Subject.Contains(query.Subject))
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Content
                if (query.Content != String.Empty)
                {
                    if (msg.TextData.TextData.Contains(query.Content))
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Answered
                if (query.Answered)
                {
                    if (msg.Flags.Answered)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Deleted
                if (query.Deleted)
                {
                    if (msg.Flags.Deleted)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Draft
                if (query.Draft)
                {
                    if (msg.Flags.Draft)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region New
                if (query.New)
                {
                    if (msg.Flags.New)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Recent
                if (query.Recent)
                {
                    if (msg.Flags.Recent)
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region AfterDate
                if (query.AfterDate != String.Empty)
                {
                    DateTime afterDate = new DateTime();
                    if (DateTime.TryParse(query.AfterDate, out afterDate))
                    {
                        if (msg.Date >= afterDate)
                        {
                            if (!result.Messages.Contains(msg))
                            {
                                result.Messages.Add(msg);
                            }
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                            {
                                result.Messages.Remove(msg);
                            }
                        }
                    }
                }
                #endregion

                #region BeforeDate
                if (query.BeforeDate != String.Empty)
                {
                    DateTime beforeDate = new DateTime();
                    if (DateTime.TryParse(query.BeforeDate, out beforeDate))
                    {
                        if (msg.Date < beforeDate)
                        {
                            if (!result.Messages.Contains(msg))
                            {
                                result.Messages.Add(msg);
                            }
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                            {
                                result.Messages.Remove(msg);
                            }
                        }
                    }
                }
                #endregion

                #region Date
                if (query.Date != String.Empty)
                {
                    DateTime date = new DateTime();
                    if (DateTime.TryParse(query.Date, out date))
                    {
                        if (msg.Date == date)
                        {
                            if (!result.Messages.Contains(msg))
                            {
                                result.Messages.Add(msg);
                            }
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                            {
                                result.Messages.Remove(msg);
                            }
                        }
                    }
                }
                #endregion

                #region Date Range
                if (query.Range != null)
                {
                    if (query.Range.DateWithinRange(msg.Date))
                    {
                        if (!result.Messages.Contains(msg))
                        {
                            result.Messages.Add(msg);
                        }
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                        {
                            result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion
            }

            return(result);
        }
示例#17
0
        /// <summary>
        /// Quickly create a search query searching only for messages who date is within the date range
        /// </summary>
        /// <param name="sdate"></param>
        /// <param name="edate"></param>
        /// <returns></returns>
        public static IMAPSearchQuery QuickSearchDateRange(DateTime sdate, DateTime edate)
        {
            IMAPSearchQuery query = new IMAPSearchQuery();
            query.Range = new DateRange(sdate, edate);

            return query;
        }
示例#18
0
        /// <summary>
        /// Private method for manually searching the cache when in offline mode
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        private IMAPSearchResult OfflineSearch(IMAPSearchQuery query)
        {
            IMAPSearchResult result = new IMAPSearchResult();
            result.Query = query;

            foreach (IMAPMessage msg in _messages)
            {
                if (msg == null)
                    continue;

                #region To
                if (query.To.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.To)
                    {
                        if (!AddressListContains(msg.To, a))
                        {
                            containsAll = false;
                            break;
                        }

                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region From
                if (query.From.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.From)
                    {
                        if (!AddressListContains(msg.From, a))
                        {
                            containsAll = false;
                            break;
                        }

                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region CC
                if (query.CC.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.CC)
                    {
                        if (!AddressListContains(msg.Cc, a))
                        {
                            containsAll = false;
                            break;
                        }

                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region BCC
                if (query.BCC.Count > 0)
                {
                    bool containsAll = true;
                    foreach (IMAPMailAddress a in query.BCC)
                    {
                        if (!AddressListContains(msg.Bcc, a))
                        {
                            containsAll = false;
                            break;
                        }

                    }

                    if (containsAll)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Subject
                if (query.Subject != String.Empty)
                {
                    if (msg.Subject.Contains(query.Subject))
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Content
                if (query.Content != String.Empty)
                {
                    if (msg.TextData.TextData.Contains(query.Content))
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Answered
                if (query.Answered)
                {
                    if (msg.Flags.Answered)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Deleted
                if (query.Deleted)
                {
                    if (msg.Flags.Deleted)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Draft
                if (query.Draft)
                {
                    if (msg.Flags.Draft)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region New
                if (query.New)
                {
                    if (msg.Flags.New)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region Recent
                if (query.Recent)
                {
                    if (msg.Flags.Recent)
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion

                #region AfterDate
                if (query.AfterDate != String.Empty)
                {
                    DateTime afterDate = new DateTime();
                    if (DateTime.TryParse(query.AfterDate, out afterDate))
                    {
                        if (msg.Date >= afterDate)
                        {
                            if (!result.Messages.Contains(msg))
                                result.Messages.Add(msg);
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                                result.Messages.Remove(msg);
                        }
                    }

                }
                #endregion

                #region BeforeDate
                if (query.BeforeDate != String.Empty)
                {
                    DateTime beforeDate = new DateTime();
                    if (DateTime.TryParse(query.BeforeDate, out beforeDate))
                    {
                        if (msg.Date < beforeDate)
                        {
                            if (!result.Messages.Contains(msg))
                                result.Messages.Add(msg);
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                                result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Date
                if (query.Date != String.Empty)
                {
                    DateTime date = new DateTime();
                    if (DateTime.TryParse(query.Date, out date))
                    {
                        if (msg.Date == date)
                        {
                            if (!result.Messages.Contains(msg))
                                result.Messages.Add(msg);
                        }
                        else
                        {
                            if (result.Messages.Contains(msg))
                                result.Messages.Remove(msg);
                        }
                    }
                }
                #endregion

                #region Date Range
                if (query.Range != null)
                {
                    if (query.Range.DateWithinRange(msg.Date))
                    {
                        if (!result.Messages.Contains(msg))
                            result.Messages.Add(msg);
                    }
                    else
                    {
                        if (result.Messages.Contains(msg))
                            result.Messages.Remove(msg);
                    }
                }
                #endregion
            }

            return result;
        }
示例#19
0
        /// <summary>
        /// Searches this folder, and optionally the sub-folders for the specified query
        /// </summary>
        /// <param name="query">IMAPSearchQuery object containing the search options</param>
        /// <returns>A new IMAPSearchResult object containing the results of the query</returns>
        public IMAPSearchResult Search(IMAPSearchQuery query)
        {
            if (_client.OfflineMode)
                return OfflineSearch(query);

            IMAPSearchResult result = new IMAPSearchResult();
            result.Query = query;
            result.Folder = this;
            string cmd = "UID SEARCH {0}\r\n";
            StringBuilder q = new StringBuilder();
            ArrayList searchTerms = new ArrayList();
            // first we need to analyze the query and build our search command
            if (query.To.Count > 0)
            {
                foreach (IMAPMailAddress a in query.To)
                {
                    if (a.DisplayName != String.Empty)
                        searchTerms.Add(String.Format("TO \"{0}\"", a.DisplayName));

                    if (a.Address != String.Empty)
                        searchTerms.Add(String.Format("TO \"{0}\"", a.Address));

                }
            }

            if (query.From.Count > 0)
            {
                foreach (IMAPMailAddress a in query.From)
                {
                    if (a.DisplayName != String.Empty)
                        searchTerms.Add(String.Format("FROM \"{0}\"", a.DisplayName));

                    if (a.Address != String.Empty)
                        searchTerms.Add(String.Format("FROM \"{0}\"", a.Address));
                }
            }

            if (query.CC.Count > 0)
            {
                foreach (IMAPMailAddress a in query.CC)
                {
                    if (a.DisplayName != String.Empty)
                        searchTerms.Add(String.Format("CC \"{0}\"", a.DisplayName));

                    if (a.Address != String.Empty)
                        searchTerms.Add(String.Format("CC \"{0}\"", a.Address));
                }
            }

            if (query.BCC.Count > 0)
            {
                foreach (IMAPMailAddress a in query.BCC)
                {
                    if (a.DisplayName != String.Empty)
                        searchTerms.Add(String.Format("BCC \"{0}\"", a.DisplayName));

                    if (a.Address != String.Empty)
                        searchTerms.Add(String.Format("BCC \"{0}\"", a.Address));
                }
            }

            if (query.Subject != String.Empty)
            {
                searchTerms.Add(String.Format("SUBJECT \"{0}\"", query.Subject));
            }

            if (query.Content != String.Empty)
            {
                searchTerms.Add(String.Format("BODY {0}", query.Content));
            }

            if (query.Date != String.Empty)
            {
                searchTerms.Add(String.Format("ON {0}", FormatDateToServer(query.Date)));
            }

            if (query.BeforeDate != String.Empty)
            {
                searchTerms.Add(String.Format("SENTBEFORE {0}", FormatDateToServer(query.BeforeDate)));
            }

            if (query.AfterDate != String.Empty)
            {
                searchTerms.Add(String.Format("SENTSINCE {0}", FormatDateToServer(query.AfterDate)));
            }

            if (query.Range != null)
            {
                searchTerms.Add(String.Format("SENTBEFORE {0}", FormatDateToServer(query.Range.EndDate)));
                searchTerms.Add(String.Format("SENTSINCE {0}", FormatDateToServer(query.Range.StartDate)));
                searchTerms.Add(String.Format("SENTON {0}", FormatDateToServer(query.Range.StartDate)));
                searchTerms.Add(String.Format("SENTON {0}", FormatDateToServer(query.Range.EndDate)));
            }

            if (query.Answered)
                searchTerms.Add("ANSWERED");

            if (query.Deleted)
                searchTerms.Add("DELETED");

            if (query.Draft)
                searchTerms.Add("DRAFT");

            if (query.New)
                searchTerms.Add("NEW");

            if (query.Recent)
                searchTerms.Add("RECENT");

            if (query.LargerThan > -1)
                searchTerms.Add(String.Format("LARGER {0}", query.LargerThan));

            if (query.SmallerThan > -1)
                searchTerms.Add(String.Format("SMALLER {0}", query.SmallerThan));

            foreach (string s in searchTerms)
            {
                q.Append(s);
                q.Append(" ");
            }

            if (!this.IsCurrentlyExamined || !this.IsCurrentlySelected)
                this.Examine();

            ArrayList cmdResult = new ArrayList();
            cmd = String.Format(cmd, q.ToString().Trim());
            if (searchTerms.Count == 0)
                throw new ArgumentNullException("No search terms were found");

            _client._imap.SearchMessage(new string[] { q.ToString().Trim() }, true, cmdResult);

            foreach (string id in cmdResult)
            {
                if (id == String.Empty)
                    continue;

                foreach (IMAPMessage msg in _messages)
                {
                    if (msg.Uid == Convert.ToInt32(id))
                    {
                        result.Messages.Add(msg);
                    }
                }
            }

            return result;
        }
示例#20
0
        /// <summary>
        /// Quickly create a search query specifying the display name and/or email address for the To: field
        /// </summary>
        /// <param name="name">display name of the person to search for</param>
        /// <param name="addr">e-mail address of the person to search for</param>
        /// <returns>IMAPSearchQuery object suitable for passing into the search method</returns>
        public static IMAPSearchQuery QuickSearchTo(string name, string addr)
        {
            IMAPSearchQuery query = new IMAPSearchQuery();
            IMAPMailAddress a = new IMAPMailAddress();
            a.Address = addr;
            a.DisplayName = name;
            query.To.Add(a);

            return query;
        }
示例#21
0
        /// <summary>
        /// Quickly create a search query specifying the subject string to search for
        /// </summary>
        /// <param name="subject">The text to search for within the subject</param>
        /// <returns>IMAPSearchQuery object suitable for passing into the search methods</returns>
        public static IMAPSearchQuery QuickSearchSubject(string subject)
        {
            IMAPSearchQuery query = new IMAPSearchQuery();
            query.Subject = subject;

            return query;
        }
示例#22
0
        /// <summary>
        /// Quickly create a search query searching only for new messages
        /// </summary>
        /// <returns>IMAPSearchQuery object suitable for passing into the search methods</returns>
        public static IMAPSearchQuery QuickSearchNew()
        {
            IMAPSearchQuery query = new IMAPSearchQuery();
            query.New = true;

            return query;
        }