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

SendGrid: Send an order receipt with a Dynamic Template

See more SendGrid Examples

Activate a version of your receipt template and set its subject to Receipt for order {{order_number}}. Try this HTML in the template:

<p>Thank you, {{first_name}}.</p>
<ul>{{#each items}}<li>{{quantity}} × {{name}}</li>{{/each}}</ul>

Replace the template ID, verified sender, and recipient. Template data is visible to the recipient. The separate, opaque notification reference is for matching delivery events to an application record; keep personal data out of custom arguments.

Dynamic Templates

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
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();

        JsonObject message = new JsonObject();
        message.updateString("from.email", "sender@example.com");
        message.updateString("template_id", "d-YOUR_ACTIVE_TEMPLATE_ID");
        message.updateString("personalizations[0].to[0].email", "recipient@example.com");
        message.updateString("personalizations[0].dynamic_template_data.order_number", "1042");
        message.updateString("personalizations[0].dynamic_template_data.first_name", "Avery");
        message.updateString("personalizations[0].dynamic_template_data.items[0].name", "Notebook");
        message.updateInt("personalizations[0].dynamic_template_data.items[0].quantity", 2);
        message.updateString("personalizations[0].dynamic_template_data.items[1].name", "Pen");
        message.updateInt("personalizations[0].dynamic_template_data.items[1].quantity", 1);
        // This reference is returned on Event Webhook events; it is not a template variable.
        message.updateString("personalizations[0].custom_args.notification_ref", "ntf_8042");

        try {
            http.httpJson("POST", "https://api.sendgrid.com/v3/mail/send", message, "application/json", 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() != 202) {
            System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
            return;
        }

        System.out.println("Accepted for delivery (HTTP 202).");
    }
}