DEV Community

Mike Hillyer for SparkPost

Posted on • Originally published at sparkpost.com on

Getting Started with C# and SparkPost

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:

PM> Install-Package SparkPost
Enter fullscreen mode Exit fullscreen mode

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

// start by creating a new SparkPost transmission
var transmission = new SparkPost.Transmission();
transmission.Content.From.Email = "me@YOURSENDINGDOMAIN.com";
transmission.Content.Subject = "Hello from SparkPost!";
transmission.Content.Text = "This is a test email.";

// add recipients who will receive your email
var recipient = new Recipient
{
    Address = new Address { Email = "you@YOUREMAILADDRESS.com" }
};
transmission.Recipients.Add(recipient);

// create a new API client using your API key
var client = new Client("<YOUR API KEY HERE>");

// if you do not understand async/await, use the sync sending mode:
client.CustomSettings.SendingMode = SendingModes.Sync;
var response = client.Transmissions.Send(transmission);

// if you understand async/await, use the default async sending mode:
// var response = client.Transmissions.Send(transmission);

// to get the transmission id back from SparkPost
// response.Result.Id;

// to get the status code of the SparkPost response
// response.Result.StatusCode;

// to get the raw json result from SparkPost
// response.Result.Content;
Enter fullscreen mode Exit fullscreen mode

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.

This post was originally published on sparkpost.com

Top comments (0)