Videos
Check out our tutorial video series.
How to send and receive email in C-sharp and .NET Core, connect to SMTP servers and use C# mail methods. This detailed guide offers practical examples and insights for developers at all levels with code examples.
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.
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.
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!
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.
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:
We can add MailSlurp and MailKit from Nuget:
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:
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.
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.
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:
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.
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.
To send email to multiple recipients in c sharp just add more recipients to the
:
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.
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:
Once we receive a confirmation email we could extract a verification code using a Regex pattern like this:
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.
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.