Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label ExactTarget. Show all posts
Showing posts with label ExactTarget. Show all posts

Thursday, July 30, 2015

How to trigger mail in ExactTarget using C# code



ExactTarget is a cloud service provided by Salesforce for running email campaign for email marketing. 

Here is a code to trigger emails in ExactTarget via its web service.


Change necessary values in below code and call the method. That’s it.

static void TriggerEmail()
{
    SoapClient client = new SoapClient();
    client.ClientCredentials.UserName.UserName = "xyz";
    client.ClientCredentials.UserName.Password = "xyzpassword";

    //Create a new subscriber - person email id
    Subscriber newSub = new Subscriber();
    newSub.EmailAddress = "abc@test.com";
    newSub.SubscriberKey = "abc@test.com";

    //Create the subscriber attributes
    newSub.Attributes = new ExactTargetClient.Attribute[4];

    //Attribute - First Name
    newSub.Attributes[0] = new ExactTargetClient.Attribute();
    newSub.Attributes[0].Name = "First Name";
    newSub.Attributes[0].Value = "Enter your first Name Here";

    //Attribute - Last Name
    newSub.Attributes[1] = new ExactTargetClient.Attribute();
    newSub.Attributes[1].Name = "Last Name";
    newSub.Attributes[1].Value = "Enter your Last Name Here";

    //Attribute - Zip Code
    newSub.Attributes[2] = new ExactTargetClient.Attribute();
    newSub.Attributes[2].Name = "Zip Code";
    newSub.Attributes[2].Value = "110011";

    //Attribute - State
    newSub.Attributes[3] = new ExactTargetClient.Attribute();
    newSub.Attributes[3].Name = "State";
    newSub.Attributes[3].Value = "Johannesburg";

    //Create a TriggerSend that uses an existing TriggerSendDefinition
    TriggeredSend ts = new TriggeredSend();
    ts.TriggeredSendDefinition = new TriggeredSendDefinition();

    //The external key assigned to the TriggerSendDeinition at ExactTarget
    ts.TriggeredSendDefinition.CustomerKey = "101"; //Keep in config file
    ClientID clientID = new ClientID();
    clientID.ID = 1234567; //Keep in config file
    ts.Client = clientID;

    //Instantiate the CreateOptions object for the create call
    CreateOptions cOptionsTS = new CreateOptions();
    cOptionsTS.RequestType = RequestType.Asynchronous;
    cOptionsTS.RequestTypeSpecified = true;

    ts.Subscribers = new Subscriber[] { newSub };

    try
    {
        //Create the TriggeredSend
        string tsRequestID = "";
        string tsStatus = "";
        CreateResult[] tsResults = client.Create(cOptionsTS, new APIObject[] { ts }, out tsRequestID, out tsStatus);
        TriggeredSendCreateResult tsCreateResults = tsResults[0] as TriggeredSendCreateResult;

        Console.WriteLine("Status Code: " + tsResults[0].StatusCode);
        Console.WriteLine("Status Message: " + tsResults[0].StatusMessage);
        Console.ReadLine();

        //Check for Error
        if (tsStatus != "OK") { throw new Exception(); }

    }
    catch (Exception exCreate)
    {
        Console.WriteLine(exCreate.Message);
        Console.ReadLine();
    }
}

Please leave your comments or share this code if it’s useful for you.