C# Requires Chilkat v11.0.0+
C#
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.
Chilkat C# Downloads
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();
success = http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5",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;
}
Chilkat.StringTable ids = new Chilkat.StringTable();
int count = reply.SizeOfArray("messages");
int i = 0;
while ((i < count)) {
reply.I = i;
success = ids.Append(reply.StringOf("messages[i].id"));
if (success != true) {
Debug.WriteLine("Could not retain a message ID.");
return;
}
i = i + 1;
}
Chilkat.StringBuilder url = 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) {
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;
}
Debug.WriteLine("Message: " + ids.StringAt(i));
int h = 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;
}
i = i + 1;
}
Debug.WriteLine("Messages inspected: " + Convert.ToString(ids.Count));