Sample code for 30+ languages & platforms
Visual Basic 6.0 Requires Chilkat v11.0.0+

Constant Contact V3: LIVE SEND - Schedule a newsletter for a named list

See more Constant Contact Examples

LIVE SEND. Scheduling this campaign delivers email to eligible contacts on the selected list at the specified time. Use the reviewer test to check the email first. Select the intended newsletter and list in the same account. The example accepts a primary draft, replaces its entire audience with one active email list, and schedules it. List lookup requires contact_data. Editing and scheduling require campaign_data and account privileges to edit and send.

Enter a future ISO 8601 timestamp with a UTC offset or Z. To cancel before delivery starts, keep the same activity selection, change action to unschedule, and run again. Cancellation removes the schedule and keeps the audience assignment. If the scheduling response is lost, check the activity’s schedule before retrying.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

'  LIVE SEND: Scheduling this activity delivers email to the selected contact list.
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim vault As New ChilkatSecrets
vault.Location = "local_manager"
vault.AutoSplit = 1
Dim entry As New ChilkatJsonObject
success = entry.UpdateString("appName","ConstantContactV3")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","my-account")
Dim tokens As New ChilkatJsonObject
Dim loaded As Long
loaded = vault.GetSecretJson(entry,tokens)
If (loaded <> 1) Then
    Debug.Print "Run the desktop authorization example using this account and store."
    Exit Sub
End If

If (tokens.JsonTypeOf("access_token") <> 1) Then
    Debug.Print "The saved authorization has no access token."
    Exit Sub
End If

Dim http As New ChilkatHttp
http.RequireSslCertVerify = 1
http.AuthToken = tokens.StringOf("access_token")
Dim resp As New ChilkatHttpResponse

'  Use the selection file from the draft or campaign lookup example for this account.
Dim selection As New ChilkatJsonObject
success = selection.LoadFile("newsletter-selection.json")
Dim activityId As String
activityId = selection.StringOf("campaign_activity_id")
If ((success <> 1) Or (activityId = "")) Then
    Debug.Print "Select a campaign and save its primary activity ID first."
    Exit Sub
End If

success = http.SetUrlVar("activity",activityId)
Dim action As String
action = "schedule"
If (action = "unschedule") Then
    success = http.HttpNoBody("DELETE","https://api.cc.email/v3/emails/activities/{$activity}/schedules",resp)
    If ((success <> 1) And (resp.StatusCode <= 0)) Then
        Debug.Print "Remove schedule: HTTP request failed."
        Exit Sub
    End If

    If ((success <> 1) Or (resp.StatusCode <> 204)) Then
        Debug.Print "Remove schedule: HTTP " & resp.StatusCode & " " & resp.BodyStr
        Exit Sub
    End If

    Debug.Print "Schedule removed for activity " & activityId
    Exit Sub
End If

If (action <> "schedule") Then
    Debug.Print "Choose schedule or unschedule."
    Exit Sub
End If

'  Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
Dim deliveryTime As String
deliveryTime = "ENTER_FUTURE_TIMESTAMP_WITH_OFFSET"
Dim delivery As New CkDateTime
Dim now As New CkDateTime
success = now.SetFromCurrentSystemTime()
success = delivery.SetFromTimestamp(deliveryTime)
If ((success <> 1) Or (delivery.DiffSeconds(now) <= 0)) Then
    Debug.Print "Enter a valid future delivery time with a UTC offset or Z."
    Exit Sub
End If

'  Use the full display name. AddParam encodes spaces and punctuation.
Dim listName As String
listName = "Workshop announcements"
Dim query As New ChilkatHttpRequest
query.HttpVerb = "GET"
query.AddParam "name",listName
query.AddParam "status","active"
query.AddParam "include_membership_count","active"
success = http.HttpReq("https://api.cc.email/v3/contact_lists",query,resp)
If ((success <> 1) And (resp.StatusCode <= 0)) Then
    Debug.Print "Look up list: HTTP request failed."
    Exit Sub
End If

If ((success <> 1) Or (resp.StatusCode <> 200)) Then
    Debug.Print "Look up list: HTTP " & resp.StatusCode & " " & resp.BodyStr
    Exit Sub
End If

Dim matches As New ChilkatJsonObject
success = matches.Load(resp.BodyStr)
If (success <> 1) Then
    Debug.Print "Expected a JSON object in the response."
    Exit Sub
End If

If (matches.SizeOfArray("lists") = 0) Then
    Debug.Print "No active email list named " & listName
    Exit Sub
End If

If ((matches.SizeOfArray("lists") <> 1) Or (matches.StringOfEquals("lists[0].name",listName,1) <> 1)) Then
    Debug.Print "Expected one exact list name."
    Exit Sub
End If

Dim listId As String
listId = matches.StringOf("lists[0].list_id")
If (listId = "") Then
    Debug.Print "Missing list ID."
    Exit Sub
End If

Debug.Print listName & " | List ID: " & listId & " | Mailable contacts: " & matches.IntOf("lists[0].membership_count")

'  PUT replaces the activity. Read it first to preserve the subject, sender and other settings.
'  Constant Contact retains omitted subresources such as html_content and the physical footer.
success = http.HttpNoBody("GET","https://api.cc.email/v3/emails/activities/{$activity}",resp)
If ((success <> 1) And (resp.StatusCode <= 0)) Then
    Debug.Print "Read draft: HTTP request failed."
    Exit Sub
End If

If ((success <> 1) Or (resp.StatusCode <> 200)) Then
    Debug.Print "Read draft: HTTP " & resp.StatusCode & " " & resp.BodyStr
    Exit Sub
End If

Dim draft As New ChilkatJsonObject
success = draft.Load(resp.BodyStr)
If (success <> 1) Then
    Debug.Print "Expected a JSON object in the response."
    Exit Sub
End If

If ((draft.StringOfEquals("role","primary_email",1) <> 1) Or (draft.StringOfEquals("current_status","DRAFT",1) <> 1)) Then
    Debug.Print "Select a primary activity in DRAFT status."
    Exit Sub
End If

Debug.Print "LIVE SEND: " & draft.StringOf("subject") & " | Activity: " & activityId & " | List: " & listName & " | Delivery: " & deliveryTime
success = draft.UpdateNewArray("contact_list_ids")
success = draft.UpdateString("contact_list_ids[0]",listId)
success = draft.UpdateNewArray("segment_ids")
success = http.HttpJson("PUT","https://api.cc.email/v3/emails/activities/{$activity}",draft,"application/json",resp)
If ((success <> 1) And (resp.StatusCode <= 0)) Then
    Debug.Print "Assign audience: HTTP request failed."
    Exit Sub
End If

If ((success <> 1) Or (resp.StatusCode <> 200)) Then
    Debug.Print "Assign audience: HTTP " & resp.StatusCode & " " & resp.BodyStr
    Exit Sub
End If

Dim updated As New ChilkatJsonObject
success = updated.Load(resp.BodyStr)
If (success <> 1) Then
    Debug.Print "Expected a JSON object in the response."
    Exit Sub
End If

If ((updated.SizeOfArray("contact_list_ids") <> 1) Or (updated.StringOfEquals("contact_list_ids[0]",listId,1) <> 1) Or (updated.SizeOfArray("segment_ids") > 0)) Then
    Debug.Print "The saved audience did not match the requested list. Scheduling stopped."
    Exit Sub
End If

Dim schedule As New ChilkatJsonObject
success = schedule.UpdateString("scheduled_date",delivery.GetAsTimestamp(0))
success = http.HttpJson("POST","https://api.cc.email/v3/emails/activities/{$activity}/schedules",schedule,"application/json",resp)
If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    Debug.Print "Schedule not confirmed. HTTP " & resp.StatusCode
    Debug.Print "The audience was saved. Check the schedule before retrying."
    Exit Sub
End If

Debug.Print "Live delivery scheduled for " & schedule.StringOf("scheduled_date") & " to " & listName