[Guide] How to Use C# to Send Email From Your Application (With Code Samples)

c# send email guide
Need an easy way to use C# to send email from your application?

In this guide, you’ll learn:

Let’s get started!

Why Your Email Infrastructure Matters When Sending Email From An Application [It’s About Deliverability]

When you’re building a web or mobile application you have a full plate…

You have to:

  • Answer to multiple stakeholders (product managers, UX, and clients)
  • Meet your sprint deadlines
  • Fix bugs
  • And maybe even do some frontend and QA work, if you’re a full stack developer

… All while shipping great code.

As a result, it’s easy to make email functionality an after-thought.

In fact, it’s not uncommon for developers to use a basic Gmail SMTP server, an Exchange Server, or an open source option like Sendmail, to quickly embed email functionality into their apps.

The problem with the options above is that they aren’t suitable for providing your app with any additional functionality beyond basic message sending.

And worse, when using the above options, your app will run into deliverability problems after its released to production – especially as your email volume starts to scale.

The last thing that any developer wants to hear from a client or manager is that their code is broken, simply because email messages from the app aren’t reaching the inbox.

That’s why your email infrastructure has become such an important part of the process to code.

If you’re looking for a way to use C# to send email, then let’s discuss how to get the job done.

Using C# to Send Email [There’s an API for That!]

When it comes to enabling your C# application to send a message, an email rest API is your best friend.

What’s an Email API?

An email API makes it easy for developers to gain access to email sending features that are offered by an email service provider, like SocketLabs.

Developers use email APIs to do everything from sending transactional email, to querying statistics about a mail stream.

To see a full list of email APIs, visit our Developer Hub.

What’s a Send Email API?

A send email API (also known as the Injection API At SocketLabs) allows developers to send email from an application.

The alternative to using an Email API is SMTP, which stands for Simple Mail Transport Protocol.

When should you use an Email API over SMTP?

An email API is best suited for situations when you need to access additional capabilities. This is when a send email API, like our Injection API, becomes useful. This API allows developers to gain programmatic access to basic email sending, and also additional capabilities not offered by traditional protocols like SMTP.

What Are the Benefits of Using an API for Sending Email?

When it comes to enabling your application to send email in C#, there are many benefits to using an API:

  • An API is the fastest and most efficient way to send email.
  • An API gives you programmatic access to email sending and additional capabilities, like capturing detailed statistics on all facets of email performance.
  • Compared to SMTP, a Web API provides an extra layer of security to your email program by utilizing API keys. Meaning that you can generate authenticated credentials separate from your account username and password, therefore reducing the risk of a malicious individual using your account to send spam.
  • And if you’re using an API that’s made available by an ESP (like SocketLabs), then you’ll also benefit from great deliverability and powerful email features such as analytics, automated bounce processing, email authentication, and more.

How to Use an Email API

When you’re ready to embed email sending functionality into your C# app, you will need to choose a Send Email API.

We suggest using the Injection API, which can be found in our Developer Hub.

Our Injection API provides you with the ability to send outbound messages through HTTP POST requests, as opposed to standard SMTP.

If you’re looking to send email in C#, then a client library will help speed up this integration.

What is a client library?

A client library, sometimes called a helper library, is a pre-written code block that you can add to your development project, in this case – your C# application. Using a library will save you hours, or even days worth of work since the code is already written for you.

This .NET/C# library allows you to easily send any type of email message supported by our Injection API, from a simple message to a single recipient, to a complex bulk message sent to multiple recipients.

Use These C# Code Samples to Enable Email Sending from Your Application

We already did most of the heavy lifting for you with these C# code samples.

Simply copy the code samples below and paste them into your application. After the code samples are added to your app, you’ll paste your server ID and API keys into the code samples.

*Note: You’ll need a SocketLabs account to obtain your Server ID and API keys. You can get a Free Account Here >>

Quick Send in C#

The QuickSend() method is a static method that allows you to quickly and easily send a message to a single recipient with only one statement!

	000001, //Your SocketLabs ServerId
	"YOUR-API-KEY", //Your Injection API Key
	"[email protected]", //The To address for your message
	"[email protected]", //The From address for your message
	"Lorem Ipsum", //The Subject line for your message
	"Lorem Ipsum", //The HTML content for your message
	"Lorem Ipsum" //The plaintext content for your message

Basic Message in C#

A basic message is an email message like you’d send from a personal email client such as Outlook. A basic message can have many recipients, including multiple To Addresses, CC Addresses, and even BCC Addresses. You can also send a file attachment in a basic message.

var client = new SocketLabsClient(serverId, apiKey);

var message = new BasicMessage();

message.Subject = "Sending A BasicMessage";
message.HtmlBody = "This is the Html Body of my message.";
message.PlainTextBody = "This is the Plain Text Body of my message.";
message.From.Email = "[email protected]";
message.To.Add("[email protected]");

var response = client.Send(message);

Bulk Message in C#

A bulk message usually contains a single recipient per message and is generally used to send the same content to many recipients, optionally customizing the message via the use of MergeData.

var client = new SocketLabsClient(000001, "YOUR-API-KEY"); //Your SocketLabs ServerId and Injection API key

var message = new BulkMessage();

message.PlainTextBody = "This is the body of my message sent to %%Name%%";
message.HtmlBody = "This is the HtmlBody of my message sent to %%Name%%";
message.Subject = "Sending a test message";
message.From.Email = "[email protected]";

var recipient1 = message.To.Add("[email protected]");

var recipient2 = message.To.Add("[email protected]");

var response = client.Send(message);

You can view all code samples here to send email using .Net or C#

For more examples and use cases visit our Github page

This wraps things up for this short guide about how to use C# to send email from your application.

In addition to C#, SocketLabs also provides code samples in the following languages:

Send Email Using PHP >>

Send Email Using Node.Js >>

Send Email Using Python >>

Send Email Using Java >>

Send Email Using Go >>

Table of Contents