Search Gmail thread id

Gmail provides a thread ID to associate groups of messages in the same manner as in the Gmail web interface.

Retrieval of this thread ID is supported via the X-GM-THRID attribute on the FETCH command.

The thread ID is a 64-bit unsigned integer.

The X-GM-THRID attribute may also be used in the SEARCH command to find the UIDs of messages in a given thread.

// 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);

    long newestMessageUID = imap.GetAll().Last();
    var threadId = imap.GetEnvelopeByUID(newestMessageUID).GmailThreadId;

    List<long> uids = imap.Search().Where(
        Expression.GmailThreadId(threadId));

    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 newestMessageUID As Long = imap.GetAll().Last()
	Dim threadId = imap.GetEnvelopeByUID(newestMessageUID).GmailThreadId

	Dim uids As List(Of Long) = imap.Search().Where( _
		Expression.GmailThreadId(threadId))

	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:
code.google.com/apis/gmail/imap/#x-gm-thrid

Tags: , , , , ,

2 Responses to “Search Gmail thread id”

  1. Gmail extensions in Mail.dll Says:

    […] Search Gmail thread id […]

  2. Create Gmail url-ID via IMAP Says:

    […] you can find some more information about how to search by X-GM-THRID and all other Gmail IMAP […]

Questions?

Consider using our Q&A forum for asking any questions.