How to send emails in C#/.NET using SMTP
Tags: Email, Dotnet, Protocol, C, Developer

Introduction

Hello Everyone, In this article, we will learn how to send emails in .NET/C# using SMTP. As sometimes, it is required to send emails to the users of the application. Email communication is considered a good way to communicate with the users of the application. There are different ways to send emails in .NET, but here we will talk about SMTP.

What is SMTP?

SMTP stands for Simple Mail Transfer Protocol. It is a communication protocol for electronic mail transmission. Mail servers and other message transfer agents use SMTP to send and receive mail messages. The Key Features of SMTP are it is considered a reliable server for sending emails, and it delivers the email more easily and quickly as it is developed from a simple platform.

Prerequisites

Let’s start

In the implementation part, we will first create a .NET Core class library, and then we will use that library in the .NET Core console app.

Step 1 — Create a .NET Core Console App project in IDE.

Step 2 — Add a .NET Core class library in the created solution.

Step 3 — Create a new class file in the class library and name it as MailArguments.cs. Add the arguments fields in this file, which will be used in sending the email.

Step 4 — Create a new class file in the class library; name it ExtensionMethods.cs. Here we will add the extension methods, which will be helpful to put the validation while sending the emails.

Resolve missing references.

Step 5 — Create a new static class file in the class library name it as Mail.cs. In this class add a static method with try and catch block.

Resolve missing references.

In this method, we are returning the Tuple<bool, string> which returns the status of the email sent and a message from the method in case of failure and success.

Step 7 — Add below lines of code which will help create the NetworkCredentials for SMTP in the created method SendEmail.

Here we are using NetworkCredential class to set the UserName and Password.

Step 8 — Add below lines of code, which will be used to initiate a message, subject, and IsBodyHtml.

Step 9 — Add the below lines of code to check and add the Headers, BCC, and Attachments. Here we have used the earlier created extension methods to add the validation.

Step 10 — Add below the line of code to create a new email address using the From and Name fields of MailArguments class.

Step 11 — Add below lines of code to create a new SMTP client and use the send method of that client. When the email is successfully sent then we are creating a new tuple for sending the success message with a true flag that will indicate mail is sent.

Here we are initializing the below variables for the SMTP client.

Step 12 — In the Program.cs, which will be in the console app project, we will initialize and add the Mail arguments which are required to send an email.

Step 13 — Before that add the reference of the class library which we have created above in your console app by doing the below steps

Right-click on the console app project->Click on Add->Click on Reference->Select the created class library project.

Step 14 — In the main method, add the below lines to initialize the arguments. Here I am using Gmail’s SMTP server port and host address.

Resolve missing references.

You have to initialize all the variables as described above. From and Password will be the Gmail’s account email id and password respectively because we are using Gmail’s SMTP server.

In the attachment, I have used the MediaTypeNames.Image.Gif, which is used for Gif Images. There are different constructors for the attachment class. For reference, you can read from here- Attachment Class. Attachments are not necessary to be passed. You can pass null in the SendMail method below.

Headers are custom headers that will be sent with the email. It is not necessary to pass these headers. You can pass null in the SendMail method below.

Step 15 — Add the below line of code to call the SendMail method of the class library.

Note

Here we have used Gmail’s SMTP server to send emails (From Mail Address) so we have to enable the Less Secure Apps in our from mail address account by enabling Allow less secure apps else we will get the error from Gmail’s SMTP server (The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.)

Conclusion

In this article, we have learned about how to How to send emails through C#/.NET using SMTP. We have used the class library to initiate in the console app to send a mail to the particular emailId.The complete code for this blog can be found at Github Repo.