IMAP Search with Free Form Text

+1 vote
asked Mar 23, 2010 by Stephen Zebo (260 points)
edited Mar 24, 2010

Can someone from the Rebex team take a look at this posting? There is no way you can do a search in the IMAP client component with free-form text. Can you please provide time frame when this feature will be included? It doesn't look like it will require too many changes, just a bit of refactoring.

Applies to: Rebex Secure Mail
commented Mar 23, 2010 by Martin Vobr (12,950 points)
Hello Stephen, Martin Vobr from Rebex here. I'll discuss it with devs and post the update here (today).

1 Answer

+1 vote
answered Mar 24, 2010 by Lukas Pokorny (128,290 points)
edited Mar 24, 2010
Best answer

What about this code? It works quite well:

// initialize, connect and authenticate an IMAP session
Imap imap = new Imap();

// select a folder

// send a search command with free-form text
imap.SendCommand("UID SEARCH", "SUBJECT test SINCE 24-Mar-2009");

// read the response
ImapResponse response = imap.ReadResponse();

// create an empty message set to fill in with message UIDs
ImapMessageSet set = new ImapMessageSet();

// get the folder's validity ID, we will need this later
long validityId = imap.CurrentFolder.ValidityId;

// parse all "SEARCH" responses
foreach (ImapResponseLine line in response.GetLines())
    if (line.Code == "SEARCH")
        // get a list of raw IDs
        string[] ids = line.Parameters.Split(' ');
        foreach (string rawId in ids)
            // convert each raw ID into Rebex-style unique ID
            long id = Convert.ToInt64(rawId);
            string uniqueId = ImapMessageSet.BuildUniqueId(validityId, id);

            // add the unique ID into the message set

// use the message set for any purpose

// for example, you can use it to retrieve the list of message envelopes
ImapMessageCollection list = imap.GetMessageList(set, ImapListFields.Envelope);

// diplay message dates and subjects
foreach (ImapMessageInfo info in list)
    Console.WriteLine("{0} {1}", info.Date, info.Subject);

If you prefer VB.NET, please let us know!

commented Mar 24, 2010 by Stephen Zebo (260 points)
This will get the job done. Thank you guys! You are awesome!
commented Mar 24, 2010 by Martin Vobr (12,950 points)
Nice to know that it works for you! Could you describe why did you chose this approach instead of using default Imap.Seach method? Maybe we overlooked something and our Search API is unsuitable for some usage scenarios. It would help us to enhance the component. Feel free to edit your question if the comment is too short.
commented Mar 24, 2010 by Stephen Zebo (260 points)
Martin, I think you should definitely consider adding the above functionality as part of your component (probably another Search method with the free-form text). The API you have provided is nice abstraction on top of the low-level IMAP search. However in order for this to be flexibly used you have to create a complete search-criteria builder/editor. The nice thing about the IMAP free-form text search is that you can provide quick-and-dirty text box entry to the end-user.