Send emails in ASP.NET Core in 5 easy steps
Sending emails is a common requirement in ASP.NET Core web applications. In
this article we will learn how to send emails with attachments using a popular
and open source component - MailKit. Let's get going.
Step 1 : Add MailKit NuGet package to ASP.NET Core project
MailKit is available as a NuGet package. Right click on your ASP.NET Core
project and click on Manage NuGet packages. Then search for MailKit. The
following figure shows the relevant entry from the NuGet package manager:
Then add the following imports in code (it could be either a controller code
or any standalone class).
Step 2 : Prepare an email message to be sent
Now that you have added MailKit to your ASP.NET Core project, it's time to
build the email message that we want to send. An email message consists of
several things such as from addresses, to address, message body and so on. Let's
begin by adding from address, to address, subject, and message boxy.
A message is represented by MimeMessage class. The following code how the
MimeMessage can be configured.
MimeMessage message = new MimeMessage();
MailboxAddress from = new MailboxAddress("Admin",
MailboxAddress to = new MailboxAddress("User",
message.Subject = "This is email subject";
The above code creates in instance of MimeMessage, a class that represents an
email message. It also uses MailboxAddress class to build a from address and a to
address. The first parameter to MailboxAddress constructor is the display name
of an email address whereas the second parameter is the email address itself.
You should change them as per your setup.
The Subject property indicates the email subject.
Step 3 : Add email body and file attachments
Next, we need to set the message body and optionally add email attachments.
The following code shows how this can be done:
BodyBuilder bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "<h1>Hello World!</h1>";
bodyBuilder.TextBody = "Hello World!";
As you can see, we make use of BodyBuilder class. It has two properties
namely HtmlBody and TextBox that hold the email's HTML version and text version
To attach one or more files to our message we need their physical file system
paths. Instead of hard-coding the paths we will compute them using
IHostingEnvironment object as shown below:
IHostingEnvironment env = null;
public HomeController(IHostingEnvironment env)
this.env = env;
bodyBuilder.Attachments.Add(env.WebRootPath + "\\file.png");
As you can see, we wish to attach file.png to our message that resides under
wwwroot. So, we injected an IHostingEnvironment object and use it's WebRootPath
property to get the physical path. Then we add the file to Attachments
collection of the BodyBuilder object we created earlier.
Once the BodyBuilder is ready we can generate a MimeMessage body from it as
message.Body = bodyBuilder.ToMessageBody();
The ToMessageBody() method creates a message body with HTML/Text content and
Step 4 : Connect and authenticate with the SMTP server
In order to send the email message we just created we need to connect with an
SMTP server. In most of the cases you will also need security credentials to
connect with the server. The following code shows how connection can be
established with an SMTP server.
SmtpClient client = new SmtpClient();
client.Connect("smtp_address_here", port_here, true);
Here, we created an instance of SmtpClient class that represents a client to
an SMTP server. We call its Connect() method and pass the SMTP server address
and SMTP port. Use these settings as per your environment. The third Boolean
parameter indicates whether the connection requires SSL or not.
We then proceed to call Authenticate() method that authenticates a user
account with the SMTP server. The Authenticate() method accepts a user name and
a password as its parameters. Change these values to suit your environment.
Step 5 : Send email message
After configuring the SMTP server connection we can now send a message and
then disconnect from the server.
The Send() message accepts an MimeMessage as a parameter and attempts to send
that message. Once a message has been sent, we can disconnect from the server
using Disconnect() method. The Boolean parameter to Disconnect() indicates that
we want to issue QUIT command to the server. Finally, we Displose() the
To test our code, fill valid values for from address, to address, and SMTP
server configuration and then attempt to run the code.
That's it for now! Keep coding!!