This post is an example how to send email in Asp.net Core 3.1 using Mailkit. I assume that you have downloaded it and install it on your server.

How to Send HTML Email in ASP.NET Core

This code sends a simple HTML email using the Ethereal free SMTP testing service, you can create a free test account in one click at https://ethereal.email/ and copy the username and password from below the title SMTP configuration. See instructions below for using different SMTP providers such as Gmail and Hotmail.

How to Send Plain Text Email in ASP.NET Core

This code sends the same email as above with a plain text body.

Change Your SMTP Provider

To change the above code to use a different email provider simply update the host parameter (the first parameter) passed to the smtp.Connect() method, for example:

Wrapping it up in an Email Service

To encapsulate the email sending functionality and make it easy to send email from anywhere in your ASP.NET Core application you can create an EmailService class and IEmailService interface like below.

Conclusion

I hope above tutorial can help you if you encounter issue when send email via your Asp.net Core application. See you on next tips!

Leave a comment

Your email address will not be published.