Search Gmail message id

Gmail provides a unique message ID for each email so that a unique message may be identified across multiple folders.

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

The message ID is a 64-bit unsigned integer.

The X-GM-MSGID attribute may also be used in the SEARCH command to find the UID of a message given Gmail’s message ID.

// 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 messageId = imap.GetEnvelopeByUID(newestMessageUID).GmailMessageId;

    List<long> uids = imap.Search().Where(
        Expression.GmailMessageId(messageId));

    MessageInfo info = imap.GetMessageInfoByUID(uids.First());

    Console.WriteLine("{0} - {1}",
        info.Envelope.GmailMessageId,
        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 messageId = imap.GetEnvelopeByUID(newestMessageUID).GmailMessageId

	Dim uids As List(Of Long) = imap.Search().Where( _
		Expression.GmailMessageId(messageId))

	Dim info As MessageInfo = imap.GetMessageInfoByUID(uids.First())

	Console.WriteLine("{0} - {1}", _
            info.Envelope.GmailMessageId, _
            info.Envelope.Subject)

	imap.Close()
End Using

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

Tags:      

Questions?

Consider using our Q&A forum for asking questions.

One Response to “Search Gmail message id”

  1. Gmail extensions in Mail.dll Says:

    […] Search Gmail message id […]