Sample code for 30+ languages & platforms
VB.NET Requires Chilkat v11.0.0+

List Gmail message IDs across all pages

See more GMail REST API Examples

List message and thread IDs for the authenticated user. Continue with nextPageToken until it is absent. A message ID is a Gmail identifier, distinct from the RFC Message-ID header. Use it in the reading examples. Spam and Trash are excluded by default.

Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat VB.NET Downloads

VB.NET
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim token As New Chilkat.JsonObject
success = token.LoadFile("gmail-token.json")
If (success <> True) Then
    Debug.WriteLine("Could not read gmail-token.json from the private working directory.")
    Exit Sub
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",True) = True)) Then
    Debug.WriteLine("An OAuth access token is required.")
    Exit Sub
End If

Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.AuthToken = token.StringOf("access_token")
Dim resp As New Chilkat.HttpResponse
Dim reply As New Chilkat.JsonObject
Dim total As Integer = 0
Dim baseUrl As New Chilkat.StringBuilder
baseUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100")
Dim url As New Chilkat.StringBuilder
Dim pageToken As New Chilkat.StringBuilder
Dim more As Boolean = True
While (more = True)
    url.SetString(baseUrl.GetAsString())
    If (pageToken.Length > 0) Then
        url.Append("&pageToken=")
        url.Append(http.UrlEncode(pageToken.GetAsString()))
    End If

    success = http.HttpNoBody("GET",url.GetAsString(),resp)
    If (resp.StatusCode = 0) Then
        Debug.WriteLine("Gmail did not return an HTTP response.")
        Exit Sub
    End If

    If ((success <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
        Debug.WriteLine("Gmail HTTP " & resp.StatusCode)
        success = reply.Load(resp.BodyStr)
        If (success = True) Then
            Debug.WriteLine(reply.StringOf("error.message"))
        End If

        Exit Sub
    End If

    success = reply.Load(resp.BodyStr)
    If (success <> True) Then
        Debug.WriteLine("Expected a Gmail JSON response.")
        Exit Sub
    End If

    '  Gmail can omit messages when the search or mailbox is empty.
    Dim count As Integer = reply.SizeOfArray("messages")
    Dim i As Integer = 0
    While (i < count)
        reply.I = i
        Debug.WriteLine(reply.StringOf("messages[i].id") & "  thread " & reply.StringOf("messages[i].threadId"))
        total = total + 1
        i = i + 1
    End While
    more = False
    If (reply.JsonTypeOf("nextPageToken") = 1) Then
        pageToken.SetString(reply.StringOf("nextPageToken"))
        more = pageToken.Length > 0
    End If

End While
Debug.WriteLine("Messages listed: " & total)