Search Gmail using Gmail’s search syntax

To provide access to the full Gmail search syntax, Gmail provides the X-GM-RAW search attribute.

Arguments passed along with the X-GM-RAW attribute when executing the SEARCH command will be interpreted in the same manner as in the Gmail web interface.

// C# version

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.gmail.com");
    imap.UseBestLogin("pat@gmail.com", "password");

    // Select 'All Mail' folder
    CommonFolders common = new CommonFolders(client.GetFolders());
    client.Select(common.AllMail);

    List<long> uids = imap.Search().Where(
        Expression.GmailRawSearch(
            "in:inbox subject:Notification with -without"));

    foreach (MessageInfo info in imap.GetMessageInfoByUID(uids))
    {
        Console.WriteLine("{0} - {1}",
            info.Envelope.GmailThreadId,
            info.Envelope.Subject);
    }

    imap.Close();
}
' VB.NET version

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.UseBestLogin("pat@gmail.com", "password")

	' Select 'All Mail' folder
	Dim common As New CommonFolders(client.GetFolders())
	client.Select(common.AllMail)

	Dim uids As List(Of Long) = imap.Search().Where( _
            Expression.GmailRawSearch( _
                "in:inbox subject:Notification with -without"))

	For Each info As MessageInfo In imap.GetMessageInfoByUID(uids)
	    Console.WriteLine("{0} - {1}",  _
                info.Envelope.GmailThreadId,  _
                info.Envelope.Subject)
	Next

	imap.Close()
End Using

You can learn more about this Gmail IMAP extension here:
http://code.google.com/apis/gmail/imap/#x-gm-raw

Tags:      

Questions?

Consider using our Q&A forum for asking questions.

One Response to “Search Gmail using Gmail’s search syntax”

  1. Gmail extensions in Mail.dll Says:

    […] Search Gmail using Gmail’s search syntax […]