How to send a transactional (Triggered Send Definition) email with ExactTarget SOAP API
25 June 2014
Why I had to make it easy
Sending a transactional email using ExactTarget is the biggest pain in the butt that I had to endure as a developer lately because setting up a new one required me to log in to ExactTarget and shaving yaks called "Data Extension", "Triggered Send", "Triggered Send Definition", "Delivery Profile", "Email Templates" etc.. I just wanted to be able to send a simple transactional email without first having to perform a number of steps in the cumbersome ExactTarget UI ....so I created this library to make my life easier when I need to set up a new transactional email. I hope this can help some poor soul out there suffering the same fate I have.
Getting started
You require an API user name, password and endpoint before you can call the SOAP API. Assuming you have those, here is a simple way of how you can send (and create) a transactional email.
Install the following from Nuget:
PM> Install-Package ExactTarget.TriggerEmailSender
Configure
var config = new ExactTargetConfiguration
{
ApiUserName = "API_User",
ApiPassword = "API_Password",
//use your endpoint given to you by ET
EndPoint = "https://webservice.s6.exacttarget.com/Service.asmx",
ClientId = 6269485//optional: business unit id you wish to use
};
If you don't have a "Triggered Send" set up yet
You can set up a new one easily without the need of logging into ExactTarget to do it.
Step 1: Setup a new email
You only need to perform this step once for each email type you want to trigger. (For example order confirmation, welcome registration, order dispatched etc)
Example
//create and start Triggered Send (only required to do this once)
var triggeredEmailCreator = new TriggeredEmailCreator(config);
triggeredEmailCreator.Create("order-confirmation",
"<html><head><style>.green{color:green}</style></head>" +
"<body><p>Dear %%FirstName%%,</p>" +
"<p>Your order has been processed</p>" +
"<p>%%OrderSummary%%</p> " +
"<p class='green'>Once again, thank you!</p>" +
"</body></html>");
triggeredEmailCreator.StartTriggeredSend("order-confirmation");
- The above example will create a TriggeredSendDefinition, Data Extension with "Subject" and the replacement values specified in your Html layout markup (%%FirstName%%, %%OrderSummary%%), Paste HTML Email Template with email and Delivery Profile for the Data Extension without header and footer in ExactTarget.
Step 2: Sending a transactional email
Now you can trigger an email. You give replacement values for Subject, Body and Head parts of the email. Head value is used to inject css into the head part of the email if you want to style the HTML version of the email.
var triggeredEmail = new ExactTargetTriggeredEmail("order-confirmation",
"recipient@temp.uri");
triggeredEmail.AddReplacementValue("Subject", "Thank you for placing your order");
triggeredEmail.AddReplacementValue("FirstName", "John");
triggeredEmail.AddReplacementValue("OrderSummary", "<ul><li>Item 1</li><li>Item 2</li></ul>");
var emailTrigger = new EmailTrigger(config);
emailTrigger.Trigger(triggeredEmail);
If you already have a "Triggered Send" set up
//the email to trigger
var triggeredEmail = new ExactTargetTriggeredEmail("external-key-of-trigger",
"recipient@uri.test" );
//specify values for the Data Extension if any (optional)
triggeredEmail.AddReplacementValue("DataExtensionFieldName1", "Value 1");
triggeredEmail.AddReplacementValue("DataExtensionFieldName2", "Value 2");
//trigger the email
var emailTrigger = new EmailTrigger(config);
emailTrigger.Trigger(triggeredEmail);