Update: The “Replace” method of templating in Fluent Email has itself been replaced by a Razor Engine Template. See Luke’s Post here about it.
I always seem to forget how to send emails in .NET which to me seems like such a simple task. Lately I’ve been working on updating an older application of mine that’s soul purpose was to read a database and send emails depending on certain events. I had a look at the current code and saw something like this:
Terrible! So I figured while I was updating it I'd work out a better way build up the HTML of the emails. I started with the FluentEmail Project (Luke’s Post about it). From there I added some basic templating features, such as loading a HTML file with placeholders for content and the rest is history…
With my recent updated to FluentEmail I was about to turn something that looked like the above code to this: (as always C# is the code of choice)
The HTML inside TransactionTemplate.htm has the below code. This is a basic example but demonstrates the usage of the templates.
Pretty damn easy if you ask me.
This also give me the flexibility to edit the HTML file in a proper editor instead of concatenating all those strings together. The main idea was inspired by the MailDefinition class built into .NET but I didnt like the face that: 1 it was part of the System.Web.UI.WebControls namespace when its not a Web Control and wouldnt be used as one. And 2, It attempted to use another Control as a worker for it. So I just extended MailMessage to add the functionality.
All you need to do is download FluentEmail from Github