C# send a mail using razor views as template

If you are looking for a way to send an e-mail via C#, you may find some code examples such as Send e-mail via SMTP using C#. I used such a solution in many projects, by creating an HTML file in a separate folder, reading its contents, do text replacements and then send the message. That solution always worked fine. I liked my old school solution also because I could edit the HTML file and preview it by clicking it or pointing the browser to its location.

Lately I found a more elegant solution for my MVC projects with a library called Postal. You can install it via NuGet, create a view containing your email template, then, in your controller, you can send a mail just with a few lines of code, as explained in the Getting Started example.

At work I coded a few mailers (to send a reminder, or an occasional newsletter) and they worked quite fast with my old school system. Also with Postal, results have been quite good.

If you are interested in performance, I can code a few tests to see how much each solution can scale. Updates will come, stay tuned.

By the way, if you are interested in creating responsive emails, you may have a look at Bootstrap Template for HTML Email and if you want to convert an existing HTML into a mail friendly HTML (all CSS styles inline), check Premailer.

Leave a Reply

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