Android™ Requires Chilkat v11.0.0+
Android™
SendGrid: Create a newsletter draft, then schedule the reviewed draft
See more SendGrid Examples
Prepare a Marketing Campaigns design with a subject, content, and an unsubscribe module. Supply the design and list UUIDs, a verified marketing sender ID, and the unsubscribe group ID. The first run creates only a draft. After reviewing the audience and content, run the scheduling branch with that same ID. Single Sends use an ISO 8601 UTC string, unlike Mail Send’s numeric Unix timestamp.
Create a Single Send · Schedule a Single Send
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkHttp http = new CkHttp();
http.put_RequireSslCertVerify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken("SENDGRID_API_KEY");
CkHttpResponse resp = new CkHttpResponse();
// Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
String reviewedDraftId = "";
if (reviewedDraftId == "") {
CkJsonObject draft = new CkJsonObject();
draft.UpdateString("name","November newsletter");
draft.UpdateString("send_to.list_ids[0]","YOUR_LIST_UUID");
draft.UpdateInt("email_config.sender_id",12345);
draft.UpdateInt("email_config.suppression_group_id",67890);
draft.UpdateString("email_config.design_id","YOUR_NEWSLETTER_DESIGN_UUID");
success = http.HttpJson("POST","https://api.sendgrid.com/v3/marketing/singlesends",draft,"application/json",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
Log.i(TAG, http.lastErrorText());
}
else {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 201) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
return;
}
CkJsonObject created = new CkJsonObject();
success = created.Load(resp.bodyStr());
if (success != true) {
Log.i(TAG, "Unexpected response: expected JSON.");
return;
}
String draftId = created.stringOf("id");
if (created.HasMember("id") != true) {
Log.i(TAG, "Missing id in response.");
return;
}
Log.i(TAG, "Draft created: " + draftId + ". Review it in Marketing Campaigns before scheduling.");
return;
}
// The example selects tomorrow at the current UTC time. Use your approved send time.
CkDateTime releaseTime = new CkDateTime();
releaseTime.SetFromCurrentSystemTime();
releaseTime.AddDays(1);
CkJsonObject schedule = new CkJsonObject();
schedule.UpdateString("send_at",releaseTime.getAsTimestamp(false));
http.SetUrlVar("draft",reviewedDraftId);
success = http.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule",schedule,"application/json",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
Log.i(TAG, http.lastErrorText());
}
else {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 201) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
return;
}
CkJsonObject scheduled = new CkJsonObject();
success = scheduled.Load(resp.bodyStr());
if (success != true) {
Log.i(TAG, "Unexpected response: expected JSON.");
return;
}
Log.i(TAG, scheduled.stringOf("status") + ": " + scheduled.stringOf("send_at"));
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}