This project is read-only.

Search command


I didnt find implementation of serach command in this library, so I wrote one.
Maybe somebody can write it better, but for me its working.

public int[] Search(string expression)
            OK - search completed
            NO - search error: can't search that [CHARSET] or criteria
            BAD - command unknown or arguments invalid

        if (!(Connection.ConnectionState == ConnectionState.Open))
        //send command
        string response, result;
            Connection.Write(string.Format("SEARCH {0}\r\n", expression));
            response = Connection.Read();
            result = Connection.Read();

            if (!response.StartsWith(@"* SEARCH"))
                throw new Exception();
                response = response.Substring(8).Trim();
            throw new Exception("Search command failed.");

        //A283 OK SEARCH completed
        Regex r = new Regex(@".*(OK|NO|BAD){1} SEARCH completed", RegexOptions.IgnoreCase);
        var matches = r.Matches(result);
        switch (matches[0].Groups[1].Captures[0].Value.ToUpper())
            case "OK":
                if (string.IsNullOrEmpty(response)) return new int[0];
                string[] ids = response.Split(' ');
                int[] ret = new int[ids.Length];
                for (int i = 0; i != ids.Length; i++)
                    ret[i] = Convert.ToInt32(ids[i]);
                return ret;
            case "NO": throw new Exception("search error: can't search that [CHARSET] or criteria");
            case "BAD": throw new Exception("command unknown or arguments invalid");
        return null;


sandeepverma wrote Sep 20, 2013 at 9:49 AM

Please suggest me how to use this search criteria in this method specially using date range to pull emails from current date - 1year or something