This might seems a naive post about sending email, especially when there are hundreds of posts out there. However, most of these posts don't show a complete or comprehensive example for the beginners on how to send email with more options. I'm sure when you search for the phrase "Sending email using C#" you will find a plenty of results, but if you look closely you will see every post focuses on one thing perhaps attachment, multiple recipients, etc.
Note: This post is intended for beginners and folks who want to copy the class and use it directly in their application :)
I remember that I wrote a class for sending email, which I still use till today, and I thought to share it with you. This class provides most of the basic functionalities that I need and it covers:
- Send Sync and Async emails.
- Send email to multiple recipients.
- Send text and HTML email.
The class provide many overloading methods for the send email, we will just focus here on two basic methods one for Sync and the other for Async. To switch between Sync and Async I created a delegate method as follow
The synchronous send email method would look like this
The Asynchronous send email method would look like this
The main method that will do the sending is the following
In the config file configure the SMTP server host
if you want you can add the follwing code to the main method above to explicitly set the SMTP host as follow
Hope this help :)