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

SendGrid: Schedule a reminder and cancel it by batch ID

See more SendGrid Examples

Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.

Create a batch ID ยท Cancel a batch

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = 0
set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
'  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken = "SENDGRID_API_KEY"
set resp = Server.CreateObject("Chilkat.HttpResponse")

'  On cancellation, set this to the batch ID saved with the original appointment.
'  This branch does not create a new batch or send another reminder.
batchToCancel = ""
If (batchToCancel <> "") Then
    set cancellation = Server.CreateObject("Chilkat.JsonObject")
    success = cancellation.UpdateString("batch_id",batchToCancel)
    success = cancellation.UpdateString("status","cancel")
    success = http.HttpJson("POST","https://api.sendgrid.com/v3/user/scheduled_sends",cancellation,"application/json",resp)
    If (success <> 1) Then
        If (resp.StatusCode = 0) Then
            Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
        Else
            Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
        End If

        Response.End
    End If

    If (resp.StatusCode <> 201) Then
        Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
        Response.End
    End If

    Response.Write "<pre>" & Server.HTMLEncode( "Batch cancellation accepted.") & "</pre>"
    Response.End
End If

success = http.HttpNoBody("POST","https://api.sendgrid.com/v3/mail/batch",resp)
If (success <> 1) Then
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    End If

    Response.End
End If

If (resp.StatusCode <> 201) Then
    Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    Response.End
End If

set batch = Server.CreateObject("Chilkat.JsonObject")
success = batch.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected response: expected JSON.") & "</pre>"
    Response.End
End If

batchId = batch.StringOf("batch_id")
If (batch.HasMember("batch_id") <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing batch_id in response.") & "</pre>"
    Response.End
End If

'  Allocate one batch per independently cancelable appointment.
Response.Write "<pre>" & Server.HTMLEncode( "Save this batch ID with the appointment: " & batchId) & "</pre>"
set sendTime = Server.CreateObject("Chilkat.CkDateTime")
success = sendTime.SetFromCurrentSystemTime()
success = sendTime.AddSeconds(3600)
set message = Server.CreateObject("Chilkat.JsonObject")
success = message.UpdateString("batch_id",batchId)
success = message.UpdateInt("send_at",sendTime.GetAsUnixTime(0))
success = message.UpdateString("from.email","sender@example.com")
success = message.UpdateString("personalizations[0].to[0].email","recipient@example.com")
success = message.UpdateString("subject","Appointment reminder")
success = message.UpdateString("content[0].type","text/plain")
success = message.UpdateString("content[0].value","Your appointment is tomorrow at 9:30 AM Central.")

success = http.HttpJson("POST","https://api.sendgrid.com/v3/mail/send",message,"application/json",resp)
If (success <> 1) Then
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( http.LastErrorText) & "</pre>"
    Else
        Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    End If

    Response.End
End If

If (resp.StatusCode <> 202) Then
    Response.Write "<pre>" & Server.HTMLEncode( "HTTP " & resp.StatusCode & ": " & resp.BodyStr) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Reminder scheduled for " & sendTime.GetAsTimestamp(0)) & "</pre>"

%>
</body>
</html>