Sample code for 30+ languages & platforms
C# 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 C# Downloads

C#
bool success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

Chilkat.JsonObject token = new Chilkat.JsonObject();
success = token.LoadFile("gmail-token.json");
if (success != true) {
    Debug.WriteLine("Could not read gmail-token.json from the private working directory.");
    return;
}

if ((token.JsonTypeOf("access_token") != 1) || (token.StringOfEquals("access_token","",true) == true)) {
    Debug.WriteLine("An OAuth access token is required.");
    return;
}

Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.AuthToken = token.StringOf("access_token");
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
Chilkat.JsonObject reply = new Chilkat.JsonObject();
int total = 0;
Chilkat.StringBuilder baseUrl = new Chilkat.StringBuilder();
baseUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100");
Chilkat.StringBuilder url = new Chilkat.StringBuilder();
Chilkat.StringBuilder pageToken = new Chilkat.StringBuilder();
bool more = true;
while ((more == true)) {
    url.SetString(baseUrl.GetAsString());
    if (pageToken.Length > 0) {
        url.Append("&pageToken=");
        url.Append(http.UrlEncode(pageToken.GetAsString()));
    }

    success = http.HttpNoBody("GET",url.GetAsString(),resp);
    if (resp.StatusCode == 0) {
        Debug.WriteLine("Gmail did not return an HTTP response.");
        return;
    }

    if ((success != true) || (resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
        Debug.WriteLine("Gmail HTTP " + Convert.ToString(resp.StatusCode));
        success = reply.Load(resp.BodyStr);
        if (success == true) {
            Debug.WriteLine(reply.StringOf("error.message"));
        }

        return;
    }

    success = reply.Load(resp.BodyStr);
    if (success != true) {
        Debug.WriteLine("Expected a Gmail JSON response.");
        return;
    }

    //  Gmail can omit messages when the search or mailbox is empty.
    int count = reply.SizeOfArray("messages");
    int i = 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;
    }

    more = false;
    if (reply.JsonTypeOf("nextPageToken") == 1) {
        pageToken.SetString(reply.StringOf("nextPageToken"));
        more = pageToken.Length > 0;
    }

}

Debug.WriteLine("Messages listed: " + Convert.ToString(total));