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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  final http = CkHttp();
  http.requireSslCertVerify = true;
  // Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
  http.authToken = 'SENDGRID_API_KEY';
  final resp = CkHttpResponse();

  // 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);
  } on ChilkatException catch (e) {
    if (resp.statusCode == 0) {
      print(e.lastErrorText);
    } else {
      print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
    }

    return;
  }

  if (resp.statusCode != 200) {
    print('HTTP ${resp.statusCode}: ${resp.bodyStr}');
    return;
  }

  final rows = CkJsonArray();
  try {
    rows.load(resp.bodyStr);
  } on ChilkatException {
    print('Unexpected response: expected JSON.');
    return;
  }

  if (rows.size == 0) {
    print('No bounce record for this address.');
    return;
  }

  final row = CkJsonObject();
  var i = 0;
  while (i < rows.size) {
    rows.objectAt2(i, row);
    print('${row.stringOf('email')}: ${row.stringOf('status')} ${row.stringOf('reason')}');
    i++;
  }
}