Generating HTML email body in C#

Exists a much better method to produce HTML email in C# (for sending out by means of System.Net.Mail), than utilizing a Stringbuilder to perform the following:

string userName = "John Doe";
StringBuilder mailBody = new StringBuilder();

I make use of NVelocity as the layout engine having previously tried utilizing the ASP.Net motor to generate html email information. NVelocity is a great deal easier to utilize while still delivering lots of adaptability. I located that using ASP.Net.aspx declare templates functioned yet had some unforeseen adverse effects.

This instance removes the HTML report, images, and attachments coming from embedded information, but making use of various other alternatives to receive streams for these elements are fine, e.g. hard-coded strings, local data, as well as so on.

That depends how complex your criteria are actually. I once possessed a request that made a table in an HTML e-mail and also I utilized an ASP.NET Gridview to leave the HTML- concatenating cords to create a table will possess been actually unpleasant.

You may would like to take a look at a few of the template structures that are accessible at the instant. A number of them are actually turn offs as an outcome of MVC but that isn’t needed. Flicker is actually a really good one.


For significant HTML that consists of the production of design attributes HtmlTextWriter is perhaps the greatest method to go. It can easily be actually a little bit cumbersome to make use of as well as some designers like the profit on its own to be effortlessly read through yet perversly HtmlTextWriter’s selections with regard impression is actually a little bit wierd. It’s open source, you can download it from

I would recomend making use of themes of some kind. There are numerous different ways to approach this yet essentially keep a design template of the Email some where (on hard drive, in a data bank etc) and simply insert the vital information (IE: Receivers name etc) in to the theme.

This is a little challenging, due to the fact that in a few other collections, HTML is actually sent out as an attachment. I propose getting rid of the PDF accessory portion coming from this example to create it more clear. Furthermore, the msg.Body is actually never ever embeded in this sample code, I presume it should be assigned the physical body variable?

I have actually carried out everything coming from getting hold of user-input as well as formatting it automaticly from different patters. The greatest service I have actually made with html emails was really xml+xslt formatting considering that we knew the input of the email up front.

Using the MailDefinition lesson is actually the incorrect technique. Yes, it comes in handy, yet it’s additionally undeveloped and depends on web UI managements– that does not make good sense for one thing that is normally a server-side job.

This is even more pliable given that it indicates you may change the layout as called for without needing to alter your code. In my take in your probably to obtain ask for improvements to the themes coming from final user. , if you desire to go the whole hog you can consist of a template editor.

Sending out handbuilt html such as this is actually probably the absolute best technique as long as the markup isn’t too complex. The stringbuilder simply begins to pay you back in phrases of productivity after about 3 concatenations, therefore for truly simple stuff strand + string are going to do.

I’ve written an article on exactly how to create HTML e-mail physical body in C# using templates making use of the MailDefinition lesson.

Leave a Reply

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