Joshi.Utils.Imap.Imap.SearchMessage C# (CSharp) Method

SearchMessage() public method

Search the messages by specified criterias
public SearchMessage ( string asSearchData, bool bExactMatch, ArrayList asSearchResult ) : void
asSearchData string Search criterias
bExactMatch bool Is it exact search
asSearchResult System.Collections.ArrayList search result
return void
        public void SearchMessage(string [] asSearchData, bool bExactMatch, ArrayList asSearchResult)
        {
            if (!m_bIsLoggedIn)
            {
                try
                {
                    Restore(true);
                }
                catch (ImapException e)
                {
                    if (e.Type != ImapException.ImapErrorEnum.IMAP_ERR_INSUFFICIENT_DATA)
                        throw e;

                    throw new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_NOTCONNECTED);
                }
            }
            if (!m_bIsFolderSelected && !m_bIsFolderExamined)
            {
                throw new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_NOTSELECTED);
            }
            int nCount = asSearchData.Length;
            if (nCount == 0)
            {
                throw new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_INVALIDPARAM);
            }
            //--------------------------
            // PREPARE SEARCH KEY/VALUE

            string sCommandSuffix = "";
            foreach (string sLine in asSearchData)
            {
                if (sLine.Length == 0)
                {
                    throw new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_INVALIDPARAM);;
                }

                // convert to lower case once for all
                sLine.ToLower();

                if (sCommandSuffix.Length > 0)
                    sCommandSuffix += " ";
                sCommandSuffix += sLine;
            }

            ImapResponseEnum eImapResponse = ImapResponseEnum.IMAP_SUCCESS_RESPONSE;
            string sCommandString = IMAP_SEARCH_COMMAND + " " + sCommandSuffix;
            sCommandString += IMAP_COMMAND_EOL;
            ArrayList asResultArray = new ArrayList();
            try
            {
                //-----------------------
                // SEND SEARCH REQUEST
                eImapResponse = SendAndReceive(sCommandString, ref asResultArray);
                if (eImapResponse == ImapResponseEnum.IMAP_SUCCESS_RESPONSE)
                {
                    //-------------------------
                    // PARSE RESPONSE
                    nCount = asResultArray.Count;
                    bool bResult = false;
                    string sPrefix = IMAP_UNTAGGED_RESPONSE_PREFIX + " ";
                    sPrefix += IMAP_SEARCH_RESPONSE;
                    foreach (string sLine in asResultArray)
                    {
                        int nPos  = sLine.IndexOf(sPrefix );
                        if (nPos != -1)
                        {
                            nPos += sPrefix.Length ;
                            string sSuffix = sLine.Substring(nPos);
                            sSuffix.Trim();
                            string [] asSearchRes = sSuffix.Split(' ');
                            foreach (string sResultLine in asSearchRes)
                            {
                                asSearchResult.Add(sResultLine);
                            }
                            bResult = true;
                            break;
                        }
                    }
                    if (!bResult)
                    {
                        throw new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_SEARCH, sCommandSuffix);
                    }
                }
                else
                    throw new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_SEARCH, asResultArray[0].ToString());
            }
            catch (ImapException e)
            {
                LogOut();
                throw e;
            }
        }

Usage Example

		public override Boolean Check ()
		{
			Imap imap = new Imap();
			imap.Login(ServerAddress, Email, Password);

			imap.ExamineFolder("Inbox");

			ArrayList messages = new ArrayList();
			imap.SearchMessage(new string[] { "UNSEEN" }, true, messages);

			for (int i = messages.Count - 1; i >= 0; i--)
			{
				if ((string)messages[i] == "")
					messages.RemoveAt(i);
			}

			return messages.Count > 0;
		}