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

Read selected headers for a page of Gmail messages

See more GMail REST API Examples

List up to five messages and fetch only From, Subject, and Date for each one. Request metadataHeaders once per header name. Read the returned headers by name because order and presence vary. This header preview leaves the MIME body on Gmail.

Authorize the application with https://www.googleapis.com/auth/gmail.metadata. 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
success = http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5",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

Dim ids As New Chilkat.StringTable
Dim count As Integer = reply.SizeOfArray("messages")
Dim i As Integer = 0
While (i < count)
    reply.I = i
    success = ids.Append(reply.StringOf("messages[i].id"))
    If (success <> True) Then
        Debug.WriteLine("Could not retain a message ID.")
        Exit Sub
    End If

    i = i + 1
End While
Dim url As New Chilkat.StringBuilder
i = 0
While (i < ids.Count)
    url.SetString("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
    url.Append(ids.StringAt(i))
    url.Append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date")
    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

    Debug.WriteLine("Message: " & ids.StringAt(i))
    Dim h As Integer = 0
    While (h < reply.SizeOfArray("payload.headers"))
        reply.I = h
        Debug.WriteLine(reply.StringOf("payload.headers[i].name") & ": " & reply.StringOf("payload.headers[i].value"))
        h = h + 1
    End While
    i = i + 1
End While
Debug.WriteLine("Messages inspected: " & ids.Count)