How to send emails in C# with SMTP?
The most common way of sending emails from your C# application is by using a Simple Mail Transfer Protocol (SMTP) server. But, as C# is not able to communicate with and instruct an SMTP host server on its own, you will need the .NET framework.
In case you are not on a first-name basis with .NET, it’s an open-source developer platform/framework also created by Microsoft. It is for building different types of applications (web, mobile, desktop, gaming, IoT, and more) and supports a range of languages, editors, and libraries.
What makes .NET so crucial in the email-sending process is the fact that it contains classes for sending emails to an SMTP server which then delivers them to recipients.
To start using .NET in your application, make sure that within it, you have the following two namespaces:
using System.Net;
using System.Net.Mail;
Then, you can use your variation of the following code example below to send your first email using C#.
Note: Although the code below can be used, it is considered somewhat outdated as it uses the SmtpClient class of the System.Net.Mail namespace instead of the MailKit library one (covered later in the article), which is more suited to modern development standards. So, if you are interested in taking only the more modern coding approach, please skip to the next section of this article.
using System;
using System.Net;
using System.Net.Mail;
namespace TestClient {
class Program
{
public static void Main (string[] args)
{
MailAddress to = new MailAddress("ToAddress");
MailAddress from = new MailAddress("FromAddress");
MailMessage email = new MailMessage(from, to);
email.Subject = "Testing out email sending";
email.Body = "Hello all the way from the land of C#";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.server.address";
smtp.Port = 25;
smtp.Credentials = new NetworkCredential("smtp_username", "smtp_password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
try
{
/* Send method called below is what will send off our email
* unless an exception is thrown.
*/
smtp.Send(email);
}
catch (SmtpException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
If you paid close attention to the code above containing the MailMessage C# example (no worries if you didn’t; we’re not in a rush :)), you will notice the use of placeholders for things such as the from and to email addresses and, more importantly, the SMTP server details.
All of these are customized. So, for instance, if you want to send an email using Gmail SMTP server, you should replace smtp.server.address with smtp.gmail.com.
Our choice for an SMTP server falls on Mailtrap’s primarily because we enjoy how easy it is to find the necessary credentials and complete the setup.
We will talk more about Mailtrap as an email delivery platform a bit later, as it could be a great solution for both testing and sending your emails. But, for the time being, it’s great to keep in mind the practicality of its SMTP server for when it comes time to decide on one yourself. At the end of the day, it’s not the most practical to send email without SMTP server.
How to send emails in C# with MailKit?
MailKit is a cross-platform .NET mail-client library facilitating not only email sending but receiving as well.
Now, you might be thinking, “Wait, wait. Didn’t you just showcase how the email-sending process is done with no involvement of MailKit?” Yes, we did. And while the code above is valid and should work without an issue in most cases, the SmtpClient class used in it might cause a few hiccups.
Why? Well, according to Microsoft and their documentation, the SmtpClient class lacks support for many modern protocols and is thus not recommended for new development. Instead, the use of MailKit or a different email library and its classes is a better alternative.
To start using MailKit, you will first need to install it via NuGet.
This is done in the Package Manager Console of Visual Studio using the following command:
Install-Package MailKit
After the installation, you can start sending emails using this C# MailKit example code with customizations, of course:
using System;
using MailKit.Net.Smtp;
using MailKit;
using MimeKit;
namespace TestClient {
class Program
{
public static void Main (string[] args)
{
var email = new MimeMessage();
email.From.Add(new MailboxAddress("Sender Name", "sender@email.com"));
email.To.Add(new MailboxAddress("Receiver Name", "receiver@email.com"));
email.Subject = "Testing out email sending";
email.Body = new TextPart(MimeKit.Text.TextFormat.Html) {
Text = "<b>Hello all the way from the land of C#</b>"
};
using (var smtp = new SmtpClient())
{
smtp.Connect("smtp.server.address", 587, false);
// Note: only needed if the SMTP server requires authentication
smtp.Authenticate("smtp_username", "smtp_password");
smtp.Send(email);
smtp.Disconnect(true);
}
}
}
}
Yes, yes, we know what you spotted in the code, the SmtpClient class. No worries, this isn’t the same class from the System.Net.Mail namespace.
This is the SmtpClient class from MailKit.Net.Smtp, which is the Microsoft-approved alternative.
How to send emails in C# with Mailtrap Email API?
What you saw demonstrated above was the old-fashioned way of sending emails in C#. And while there is nothing wrong with it, it certainly isn’t as advanced and effortless as sending with third-party email services, which more often than not, provide automation features, analytics, and more.
When it comes to picking these types of services, you might want to consider Mailtrap Email API – the email-sending side of the Mailtrap email delivery platform and the optimal choice for those who want to have more control over their email deliverability.
After going through the secure and smooth setup, you can use Email API to send your emails and make them land in people’s inboxes instead of their spam folders.
Email API makes this possible by giving you tools to spot and fix early sending issues.
To be more specific, those who decide on using Email API as their third-party email service gain access to actionable analytics features, which include daily and weekly deliverability alerts, up to 60-day email logs, webhooks, and dashboards with critical alerts.
Among the listed features, what might be the most interesting are the color-coded critical alert dashboards which can be filtered by top mailbox providers, thus enabling you to dig deeper to improve your email performance.