Is there a better way to generate HTML email in C# (for sending via System.Net.Mail), than using a Stringbuilder to do the following:
string userName = "John Doe";
StringBuilder mailBody = new StringBuilder();
mailBody.AppendFormat("<h1>Heading Here</h1>");
mailBody.AppendFormat("Dear {0}," userName);
mailBody.AppendFormat("<br />");
mailBody.AppendFormat("<p>First part of the email body goes here</p>");
and so on, and so forth?
There is similar StackOverflow question that contains some fairly comprehensive responses. Personally I use NVelocity as the template engine having previously tried using the ASP.Net engine to generate html email content. NVelocity is a lot simpler to use while still providing tons of flexibility. I found that using ASP.Net .aspx files for templates worked but had some unanticipated side effects.
I would recomend using templates of some sort. There are various different ways to approach this but essentially hold a template of the Email some where (on disk, in a database etc) and simply insert the key data (IE: Recipients name etc) into the template.
This is far more flexible because it means you can alter the template as required without having to alter your code. In my experience your likely to get requests for changes to the templates from end users. If you want to go the whole hog you could include a template editor.
You can use the MailDefinition class.
This is how you use it:
Also, I've written a blog post on how to generate HTML e-mail body in C# using templates using the MailDefinition class.
I use dotLiquid for exactly this task.
It takes a template, and fills special identifiers with the content of an anonymous object.
It also works with collections, see some online examples.