Using SparkPost Templates with C#
Tags: csharp, email, howto, c
Published: 2017-10-04 17:49:25

---
title: "Using SparkPost Templates with C#"
published: true
tags: csharp,email,howto,c#
canonical_url: https://www.sparkpost.com/blog/sparkpost-templates-csharp/
---

If you take a look at the workhorse transactional email in your inboxâ€”things like receipts, shipping notices, password resets, and the like â€” I bet youâ€™ll notice thereâ€™s an awful lot of ugly emails. A company will spend time and money on a brand, on designing a compelling website, and on advertising, but when itâ€™s time for their automated systems to send an email to the user, you get something like this:

![](https://media.sparkpost.com/uploads/2017/09/Pasted-image-at-2017_09_11-01_44-PM.png)

All that work, and itâ€™s thrown away on a plain-text email with no design at all. Why? **Because design and code donâ€™t mix well** &nbsp;together. Most automated messages are built line by line in plain text and then sent using a .Send() function. Marketers donâ€™t like it because they canâ€™t update the look and feel of the messages to match the rest of their work and developers donâ€™t like it because it clutters their code. They also end up getting constantly asked to change a line here, a word there, etc.

There is a better way. SparkPostâ€™s built-in templating capabilities allow for a separation of design and code. Instead of trying to embed the content of your messages into your code, you can simply and easily upload your templates to SparkPost and call our APIs with a template reference rather than passing the formatted message content. Not only is this easier than techniques such as parsing Razer pages to strings, but it also moves the template out of the application completely, leaving the developer to focus on code.

Letâ€™s get started!

### Sending a Template Email Through SparkPost with C#

If youâ€™re following along at home, we started with a quick example of [how to send a C# email using the SparkPost](https://dev.to/sparkpost/getting-started-with-c-and-sparkpost)API (and if youâ€™re not, read it now to see how easy it is to send a C# email message using SparkPost, Iâ€™ll wait). Our next goal is to remove the message design elements from our code and replace them with a stored template.

First, we need to create and store a template. Start by reading [our guide to managing templates](https://www.sparkpost.com/docs/getting-started/creating-template/) and set up a template. For this example, Iâ€™ll use the following HTML template in a template named â€œMy First Emailâ€:

```
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>Hi {{ Name }};</p>
<p>Here are our upcoming events:</p>
<table border="1">
 <tr>
  <th>Name</th>
  <th>Date</th>
 </tr>
 {{ each UpcomingEvents }}
  <tr>
   <td>{{ loop_vars.Name }}</td>
   <td>${{ loop_vars.Date }}</td>
  </tr>
 {{end}}
</table>
</body>
</html>
```

The SparkPost templating language is based on [HandleBars](http://handlebarsjs.com/) and is straightforward to use. In this case, substitutions occur inside the {{ }} blocks, and SparkPost templating supports logic, basic operations, and even looping through arrays. For more information, see the [Substitutions Reference](https://developers.sparkpost.com/api/substitutions-reference.html) in our API Documentation.

Once our template is stored and published, we can use it in a call to the transmissions API:

```
var transmission = new Transmission();
transmission.Content.From.Email = "testing@YOURDOMAIN.com";
transmission.Content.TemplateId = "my-first-email";
var firstRecipient = new Recipient
{
	Address = new Address { Email = "you@YOURDOMAIN.com" }
};
var secondRecipient = new Recipient
{
	Address = new Address { Email = "second@YOURDOMAIN.com" }
}
// template data can be passed on a per-recipient basis
firstRecipient.SubstitutionData["Name"] = "Mike";
secondRecipient.SubstitutionData["Name"] = "Bob            	
transmission.Recipients.Add(firstRecipient);
transmission.Recipients.Add(secondRecipient);
// template data can also be passed on a per-transmission basis
transmission.SubstitutionData["UpcomingEvents"] = new []
{
new { Name = "Thanksgiving", DueDate = new Date(2017, 11, 23) },
	new { Name = "Christmas", DueDate = new Date(2017, 12, 25) }
};

var client = new Client("<YOUR API KEY>");
var response = await client.Transmission.Send(transmission);
```

And thatâ€™s it. We create a transmission, create a recipient, add template substitution data to the recipient, add the recipient to the transmission, and send the transmission to the SparkPost API. If you wanted to send to more than one recipient, you would simply add more recipients to the transmission before sending.

Your code is focused on triggering the message and filling in the blanks, the design is completely separated out and not a concern. Is it less ugly than the example that I started with? Well, no (Iâ€™m no designer), but at least now your design team can log into the SparkPost web UI and make it prettier without asking you to change code to improve the design!

### Next up on your journey

Youâ€™ve taken the next step to being a C# email master by separating design from sending logic. You have a working template, youâ€™ve sent it in a message, and your design team can adjust the look and feel of the message without your involvement!

Once again, your journey is not yet complete. Is it enough to send a message, without having any idea whether people are receiving it? No! Stay tuned, and I will show you how to query the metrics API to determine how your sending has gone.

Till then, [sign up for SparkPost](https://pages.sparkpost.com/bl-account-signup-generic.html?src=Blog&pc=bl-rfi-generic-2017&utm_source=blog&utm_medium=blog&utm_campaign=all&utm_content=signup), [learn how to manage templates via API](https://developers.sparkpost.com/api/templates.html), and check out our&nbsp;[great blogs on templates](https://www.sparkpost.com/blog/category/feature/templates/)! If you have any questions come to the #csharp channel in [community slack](http://slack.sparkpost.com).



The post [Using SparkPost Templates with C#](https://www.sparkpost.com/blog/sparkpost-templates-csharp/) appeared first on [SparkPost](https://www.sparkpost.com).