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

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonArray;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        success = false;
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        // Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
        http.setAuthToken("SENDGRID_API_KEY");
        HttpResponse resp = new HttpResponse();

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

        try {
            http.httpNoBody("GET", "https://api.sendgrid.com/v3/suppression/bounces/{$address}", resp);
        } catch (ChilkatException e) {
            if (resp.getStatusCode() == 0) {
                System.out.println(e.getLastErrorText());
            } else {
                System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
            }

            return;
        }

        if (resp.getStatusCode() != 200) {
            System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
            return;
        }

        JsonArray rows = new JsonArray();
        try {
            rows.load(resp.getBodyStr());
        } catch (ChilkatException e) {
            System.out.println("Unexpected response: expected JSON.");
            return;
        }

        if (rows.getSize() == 0) {
            System.out.println("No bounce record for this address.");
            return;
        }

        JsonObject row = new JsonObject();
        int i = 0;
        while (i < rows.getSize()) {
            rows.objectAt2(i, row);
            System.out.println(row.stringOf("email") + ": " + row.stringOf("status") + " " + row.stringOf("reason"));
            i = i + 1;
        }
    }
}