CSharp is a powerful language with many features including the ability to send and read emails. In this tutorial we'll explain everything there is to know about email and DotNET and show you how to send and receive mail and attachments in C#. We'll even cover some extra topics like email validation, attachments, and waiting for emails in tests. A quick example looks like this:

Create a free SMTP mail-server on MailSlurp to send email in CSharp.

How does email work in C#

In C#, sending an email involves interacting with the Simple Mail Transfer Protocol (SMTP), the standard protocol for sending emails across the Internet. Developers use SMTP to communicate with mail servers, which are responsible for forwarding emails to the recipient's mail server. The process is akin to sending a letter through traditional mail, where SMTP servers act as the postal system for electronic messages.

YouTube video thumbnail

SMTP only allows of email sending so in order to read emails we can either connect to a server via IMAP (Internet Message Access Protocol) or use a library like MailSlurp to wait for emails to arrive and fetch them in our C-sharp code. We can also use MailSlurp to improve email delivery with csharp email validation. Let's get started!

Common libraries

For csharp send email methods, developers typically use the namespace, which provides classes like for managing the connection to an SMTP server, and for creating the email content.

Alternatively, is another popular csharp email library with more features and flexibility around complex emails and attachments. Both libraries require a configuration with SMTP access details such as a server port, host, username and password. Let's do that now.

Creating a mailserver

In order to connect to an SMTP server we need to access a mailserver. To do this we can use the free MailSlurp Email API to spin up disposable email accounts that we can connect to using and . Firstly, let us install and configure MailSlurp:

Install packages

We can add MailSlurp and MailKit from Nuget:

Import and configure

Now we can import the required packages like so:

Next we need to set an API key for the MailSlurp client. Get a free API KEY from the MailSlurp dashboard then set the header in your config setup:

Create a new email address

Great, now we can generate a disposable mailserver for our c sharp code example:

Notice the access details. We will use those details to connect over SMTP protocol to the server using MailKit.

Sending an email

Now that we have an inbox we can send from it using a . Let us construct one like this:

We can then send the message using the MailKit SMTPClient:

Excellent! We just sent our first email using a MailSlurp SMTP server and the MailKit library. Now let's try to receive one.

Check email

In c# check email using either IMAP or the MailSlurp API. In csharp read email using the to wait for the most recent email in the inbox we created. This keeps the connection open until our desired email is received.

This code shows us how to receive an email. We can then read the email body, subject, attachments and headers directly from the variable. We can also fetch the email using the method once it has arrived:

Attachments and images

In C# embed image in email using the MailKit class. This constructs a multipart mime-message and embeds your image in an email.

Then send the email using the same SMTPClient send methods.

Validate email addresses

To improve delivery and reduce bounce rates, it's crucial to validate email addresses before sending emails. This is how we use C# validate email methods in the MailSlurp API to clean email lists:

This process, often referred to as csharp validate email address, involves checking the syntax of email addresses and can also extend to verifying whether an email address exists and can receive emails.

Send with multiple recipients

To send email to multiple recipients in c sharp just add more recipients to the :

Testing with email

Many applications use email. We can use APIs like MailSlurp to test these processes. For example disposable email accounts can be created in C-sharp to test user login and receive email verification codes.

Waiting for email in tests

Imagine a Selenium test that loads a webapp and signs up for a new account. We could use the MailSlurp wait for controller to wait for that email to arrive like so:

Extract verification codes and confirm

Once we receive a confirmation email we could extract a verification code using a Regex pattern like this:

More options

There is so much you can do with C# and email. Check out these links for more information or see the MailSlurp email examples repository on GitHub.