How to Send Email in NET Core SMTP vs MailKit

Today in this article, we will see how to Send Email in .NET Core using multiple approaches like SMTP and MailKit

I was looking to build an Email service in .NET Core recently.

This service’s core functionality was to send emails using the registered SMTP server.

Initially, I thought it would be challenging to implement using .NET Core but after some analysis,

I found that .NET Core has full support for email functionality using traditional classes like SmtpClient and MailMessage.

Today in this article, we will cover below aspects,

Today in this example, I will be using the same traditional classes and SMTP server to implement Send email functionality.

It’s recommended to use MailKit or MimeKit for any new requirements. Please see below for more details.

To make it even simpler I have used my own Gmail( modified below) for sending an email and testing purposes which worked very well after some initial travail.

Getting Started

Create .NET Core

SMTP is fully supported for .NET Core. You could use any version of the latest .NET Core.

SMTP – Send Email

We shall be using the POST method for sending out mail. Let’s write the below code for the POST method.

        [HttpPost]
        public ActionResult<IEnumerable<bool>> SendEmail([FromBody] string emailData)
        {
            try
            {
                using (var mailMessage = new MailMessage())
                {
                    using (var client = new SmtpClient("smtp.gmail.com", 587))
                    {
                        //provide credentials
                        client.Credentials = new NetworkCredential("abcdefgh@gmail.com", "passsword");
                        client.EnableSsl = true;

                        // configure the mail message
                        mailMessage.From = new MailAddress("abcdefgh@gmail.com");
                        mailMessage.To.Insert(0, new MailAddress("abcdefgh@gmail.com"));
                        mailMessage.Subject = "Learn SMTP sending mail in .NET Core";
                        mailMessage.Body = emailData ;//"You did it ";

                        //send email
                        client.Send(mailMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return StatusCode(500);
            }

            return Ok(true);
        }

Here I have used the Gmail SMTP server for sending out mail to my own Gmail id.

SMTP – Server error troubleshooting

Initially, I got the below error,

" The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. "

Such errors are more due to the policy configured on the SMTP email server.

Make sure you are following the proper settings and policies required for a secure connection to connect to the SMTP server.

For the Gmail SMTP server above code worked perfectly for me except I had to update a few settings as mentioned below. This is just for your additional information.

  • The first time I tried to log in with my email ID and password, I got the below error,

How to Send Email in NET Core SMTP vs MailKit

I also got an email from Gmail saying “Critical security alert for your linked Google Account”

  • I had to enable “Allow less secure apps: ON” to make the send email API work properly.

3. Finally, an email popped up successfully in my Inbox !!

Send Email in NET Core

As per Microsoft SmtpClient is good for one-off emails from tools but doesn’t scale to modern requirements of the protocol. So if the requirement is simple enough to send an email, this approach will still work for you.

It’s recommended to use MailKit or MimeKit or other libraries for any additional protocol requirements.

Other references,

Happy Coding !!

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *