C# Requires Chilkat v11.0.0+
C#
SendGrid: Save and verify a topic opt-out
See more SendGrid Examples
Use this when a signed-in reader turns off Product updates. Look up the configured group before submitting: SendGrid warns that a nonexistent or deleted group can cause a global suppression instead. Keep the group active while processing preferences. Deleting a marketing contact is not a substitute for recording an opt-out.
Add group suppressions ยท Check specific addresses
Chilkat C# Downloads
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY";
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
// Take the address from the authenticated account and the group ID from configuration.
string address = "reader@example.com";
http.SetUrlVar("group","12345");
success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/asm/groups/{$group}",resp);
if (success != true) {
if (resp.StatusCode == 0) {
Debug.WriteLine(http.LastErrorText);
}
else {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode != 200) {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
return;
}
Chilkat.JsonObject groupInfo = new Chilkat.JsonObject();
success = groupInfo.Load(resp.BodyStr);
if (success != true) {
Debug.WriteLine("Unexpected response: expected JSON.");
return;
}
if (groupInfo.StringOfEquals("name","Product updates",true) != true) {
Debug.WriteLine("Unexpected unsubscribe group; no preference was changed.");
return;
}
Chilkat.JsonObject addresses = new Chilkat.JsonObject();
addresses.UpdateString("recipient_emails[0]",address);
success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions",addresses,"application/json",resp);
if (success != true) {
if (resp.StatusCode == 0) {
Debug.WriteLine(http.LastErrorText);
}
else {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode != 201) {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
return;
}
success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",addresses,"application/json",resp);
if (success != true) {
if (resp.StatusCode == 0) {
Debug.WriteLine(http.LastErrorText);
}
else {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
}
return;
}
if (resp.StatusCode != 200) {
Debug.WriteLine("HTTP " + Convert.ToString(resp.StatusCode) + ": " + resp.BodyStr);
return;
}
Chilkat.JsonArray matches = new Chilkat.JsonArray();
success = matches.Load(resp.BodyStr);
if (success != true) {
Debug.WriteLine("Unexpected response: expected JSON.");
return;
}
if (matches.Size == 1) {
Debug.WriteLine("Confirmed opt-out: " + matches.StringAt(0));
}
else {
Debug.WriteLine("Opt-out not confirmed. Recheck this group.");
}