Skip to content

Example: create and send a campaign

Stefano Varesi edited this page Apr 9, 2015 · 1 revision
package com.contactlab.api.ws.examples;

import com.contactlab.api.ws.ClabService;
import com.contactlab.api.ws.ClabService_Service;
import com.contactlab.api.ws.domain.AuthToken;
import com.contactlab.api.ws.domain.Campaign;
import com.contactlab.api.ws.domain.DeliveryStatus;
import com.contactlab.api.ws.domain.EmailMessage;
import com.contactlab.api.ws.domain.Message;
import com.contactlab.api.ws.domain.Recipients;
import com.contactlab.api.ws.domain.Sender;

public class SendNewCampaign {

    public static void main(String[] args) throws InterruptedException {

        ClabService clabService = new ClabService_Service().getClabServicePort();

        AuthToken token = clabService.borrowToken(Parameters.apiKey, Parameters.userKey);

        Sender sender = new Sender();
        sender.setName("John Doe");
        sender.setEmail("[email protected]");

        int subscriberSourceId = 1;
        int subscriberSourceFilterId = 1;
        int modelIdentifier = 1;

        Recipients recipients = new Recipients();
        recipients.setSubscriberSourceFilterIdentifier(subscriberSourceFilterId);
        recipients.setSubscriberSourceIdentifier(subscriberSourceId);

        Message Message = clabService.getMessageModelById(token, modelIdentifier);
        // Casting is due to the fact that getMessageModelById
        // returns a plain Message
        EmailMessage emailMessage = (EmailMessage) Message;
        emailMessage.setSender(sender);
        emailMessage.setSubject("A Sample Mail");
        emailMessage.setHtmlContent("<html><body>Hello, <br/>"
            + "What about a pizza tomorrow? See you by the central station "
            + "around 1PM.</body></html>");
        emailMessage.setTextContent("Your email client is not able "
            + "to properly render this content, "
            + "view it online at: ${online_version_url}$");
        emailMessage.setRecipients(recipients);

        Campaign campaign = new Campaign();
        campaign.setName("Sample Campaign");
        campaign.setMessage(emailMessage);

        campaign = clabService.createCampaign(token, campaign);

        clabService.sendCampaign(token, campaign.getIdentifier());

        DeliveryStatus status;

        while (true) {
            status = clabService.getCampaignDeliveryStatus(token, campaign.getIdentifier());
            if (status == DeliveryStatus.FINISHED
            || status == DeliveryStatus.FAILED
            || status == DeliveryStatus.INTERRUPTED) {
                break;
            }
            System.out.println("wait...");
            Thread.sleep(60000);
        }

        System.out.println("Campaign ended with status " + status);

        clabService.invalidateToken(token);
    }

}