Transform your New User Welcome emails with C# and SparkPost Click to Tweet
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:

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 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 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 and set up a template. For this example, I’ll use the following HTML template in a template named “My First Email”:

The SparkPost templating language is based on HandleBars 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 in our API Documentation.

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

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, learn how to manage templates via API, and check out our great blogs on templates! If you have any questions come to the #csharp channel in community slack.

— Mike

 

 

There’s a Better Way to Send Email in C#

Sending email is a very common need for C# applications. Think about some of the use cases almost every app needs to handle: new user welcomes, event notifications, password resets, and so on. These transactional emails are workhorses, and it’s really important that they get to your users. As a C# developer, what’s the best way to get them done?

There are several C# email libraries out there, including system.net.mail, systems.web.mail. SmtpClient, MailKit, MimeKit, etc., but they all have one thing in common: you need an SMTP server to use them. Even if you have access to a server, it’s probably not tuned to ensure your messages get to your user’s inbox quickly and consistently. Using the SparkPost email delivery service is an easier way.

Let’s give it a try. Here’s how to send a message using SparkPost and C#.

Hello, World! I’m Sending Email with C# and SparkPost

The first step in sending email using C# with SparkPost is to sign up for a SparkPost developer account and get your sending domain configured. Follow our Getting Started Guide to get your account set up right with a sending domain. I’ll wait right here.

With your account set up, your sending domain configured, and your API key in hand, you’re ready to send your first email using C#. Create an application and install Sparkpost Nuget package from your Nuget Package Manager Console:

With SparkPost installed, we can format and send our first C# email message:

And that’s it! Launch the application and you should see a test message in your inbox. And the best part? You don’t have to manage any servers, monitor any services, or worry about whether the message will reach its recipient. You take care of deciding what to send and to whom, and SparkPost takes care of the rest.

Stay Tuned for More C# Email Tips

You’re well on your way to being a C# email master, thanks to one simple API. You have a working sending domain, you’ve sent your first message, and soon you will conquer the email world!

That said, your journey is not yet complete. Do you really want to have to combine content and design by building your email templates in code? Of course not! Stay tuned and I will show you how to separate the content of the message out of your API call and pre-store the message template, simplifying your code even further.

Till then, sign up for SparkPost, set up your domain, and check out our great API documentation! If you have any other questions around C# and SparkPost, come to the #csharp channel in our community slack.

— Mike