Create Gmail url-ID via IMAP
Tuesday, August 30th, 2011This is Gmail link that points to certain conversation:
https://mail.google.com/mail/u/0/#inbox/13216515baefe747
“13216515baefe747″ is the Gmail thread-ID in hex.
Here’s the code that:
- Selects “All Mail” folder
- Gets the newest message UID
- Obtains Gmail thread ID for this message (X-GM-THRID)
- Converts it to hex
- Creates the url that point to the Gmail conversation
// C# version
using (Imap client = new Imap())
{
client.ConnectSSL("imap.gmail.com");
client.Login("pat@gmail.com", "password");
// select "All Mail" folder
List<FolderInfo> allFolders = client.GetFolders();
FolderInfo allMail = new CommonFolders(allFolders).AllMail;
client.Select(allMail);
// get IMAP uid of the newest message
long lastUid = client.GetAll().Last();
// get message info
MessageInfo info = client.GetMessageInfoByUID(lastUid);
// extract Gmail thread ID
Int64 threadId = Int64.Parse(info.Envelope.GmailThreadId);
string threadIdAsHex = threadId.ToString"X");
// create url
string url = string.Format(
"https://mail.google.com/mail/u/0/#inbox/{0}",
threadIdAsHex);
Console.WriteLine(url);
client.Close();
}
' VB.NET version
Using client As New Imap()
client.ConnectSSL("imap.gmail.com")
client.Login("pat@gmail.com", "password")
' select "All Mail" folder
Dim allFolders As List(Of FolderInfo) = client.GetFolders()
Dim allMail As FolderInfo = New CommonFolders(allFolders).AllMail
client.[Select](allMail)
' get IMAP uid of the newest message
Dim lastUid As Long = client.GetAll().Last()
' get message info
Dim info As MessageInfo = client.GetMessageInfoByUID(lastUid)
' extract Gmail thread ID
Dim threadId As Int64 = Int64.Parse(info.Envelope.GmailThreadId)
Dim threadIdAsHex As String = threadId.ToString("X")
' create url
Dim url As String = String.Format( _
"https://mail.google.com/mail/u/0/#inbox/{0}", _
threadIdAsHex)
Console.WriteLine(url)
client.Close()
End Using
Here you can find some more information about how to search by X-GM-THRID and all other Gmail IMAP extensions.

