PowerShell Requires Chilkat v11.0.0+
PowerShell
Inspect the MIME parts of a Gmail message
See more GMail REST API Examples
Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.
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.
Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
$token = New-Object Chilkat.JsonObject
$success = $token.LoadFile("gmail-token.json")
if ($success -ne $true) {
$("Could not read gmail-token.json from the private working directory.")
exit
}
if (($token.JsonTypeOf("access_token") -ne 1) -or ($token.StringOfEquals("access_token","",$true) -eq $true)) {
$("An OAuth access token is required.")
exit
}
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.AuthToken = $token.StringOf("access_token")
$resp = New-Object Chilkat.HttpResponse
$reply = New-Object Chilkat.JsonObject
$url = New-Object Chilkat.StringBuilder
$url.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
$url.Append("GMAIL_MESSAGE_ID")
$url.Append("?format=full")
$success = $http.HttpNoBody("GET",$url.GetAsString(),$resp)
if ($resp.StatusCode -eq 0) {
$("Gmail did not return an HTTP response.")
exit
}
if (($success -ne $true) -or ($resp.StatusCode -lt 200) -or ($resp.StatusCode -ge 300)) {
$("Gmail HTTP " + $resp.StatusCode)
$success = $reply.Load($resp.BodyStr)
if ($success -eq $true) {
$($reply.StringOf("error.message"))
}
exit
}
$success = $reply.Load($resp.BodyStr)
if ($success -ne $true) {
$("Expected a Gmail JSON response.")
exit
}
if ($reply.JsonTypeOf("payload") -ne 3) {
$("Expected a MIME payload.")
exit
}
# Queue paths into the original JSON document so any MIME nesting depth is supported.
$paths = New-Object Chilkat.StringTable
$success = $paths.Append("payload")
if ($success -ne $true) {
$("Could not queue the root part.")
exit
}
$part = New-Object Chilkat.JsonObject
$childPath = New-Object Chilkat.StringBuilder
$p = 0
while (($p -lt $paths.Count)) {
$success = $reply.ObjectOf2($paths.StringAt($p),$part)
if ($success -ne $true) {
$("Could not read a MIME part.")
exit
}
$("Part: " + $part.StringOf("mimeType") + " bytes: " + $part.IntOf("body.size"))
if ($part.JsonTypeOf("filename") -eq 1) {
if ($part.StringOfEquals("filename","",$true) -ne $true) {
$("Filename: " + $part.StringOf("filename"))
}
}
if ($part.JsonTypeOf("body.attachmentId") -eq 1) {
$("Attachment ID: " + $part.StringOf("body.attachmentId"))
}
else {
if ($part.JsonTypeOf("body.data") -eq 1) {
$("Body data is available in this part.")
}
}
$j = 0
while (($j -lt $part.SizeOfArray("parts"))) {
$childPath.SetString($paths.StringAt($p))
$childPath.Append(".parts[")
$childPath.AppendInt($j)
$childPath.Append("]")
$success = $paths.Append($childPath.GetAsString())
if ($success -ne $true) {
$("Could not queue a child part.")
exit
}
$j = $j + 1
}
$p = $p + 1
}