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

SendGrid: Look up a bounced email address

See more SendGrid Examples

Look up a recipient on the bounce suppression list without changing it. SendGrid returns an array. An empty array means there is no bounce record for this lookup; it does not prove a particular message reached the inbox.

Retrieve a bounce

Chilkat PowerShell Downloads

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = $false
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
#  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$http.AuthToken = "SENDGRID_API_KEY"
$resp = New-Object Chilkat.HttpResponse

#  SetUrlVar encodes the raw address as a URL component, including the plus sign.
$http.SetUrlVar("address","avery+orders@example.com")

$success = $http.HttpNoBody("GET","https://api.sendgrid.com/v3/suppression/bounces/{$address}",$resp)
if ($success -ne $true) {
    if ($resp.StatusCode -eq 0) {
        $($http.LastErrorText)
    }
    else {
        $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
    }

    exit
}

if ($resp.StatusCode -ne 200) {
    $("HTTP " + $resp.StatusCode + ": " + $resp.BodyStr)
    exit
}

$rows = New-Object Chilkat.JsonArray
$success = $rows.Load($resp.BodyStr)
if ($success -ne $true) {
    $("Unexpected response: expected JSON.")
    exit
}

if ($rows.Size -eq 0) {
    $("No bounce record for this address.")
    exit
}

$row = New-Object Chilkat.JsonObject
$i = 0
while (($i -lt $rows.Size)) {
    $rows.ObjectAt2($i,$row)
    $($row.StringOf("email") + ": " + $row.StringOf("status") + " " + $row.StringOf("reason"))
    $i = $i + 1
}