<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Martin Normark&#039;s blog &#187; Hidden features of .NET</title>
	<atom:link href="http://martinnormark.com/tag/hidden-features-of-net/feed" rel="self" type="application/rss+xml" />
	<link>http://martinnormark.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 21:06:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Generate HTML e-mail body in C# using templates</title>
		<link>http://martinnormark.com/generate-html-e-mail-body-in-c-using-templates?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/generate-html-e-mail-body-in-c-using-templates#comments</comments>
		<pubDate>Sat, 13 Mar 2010 23:42:00 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Hidden features of .NET]]></category>

		<guid isPermaLink="false">/post/Generate-HTML-e-mail-body-in-C-using-templates.aspx</guid>
		<description><![CDATA[Almost a year ago, I answered a question on StackOverflow, about if there’s a better way to generate HTML e-mails in C# than using a StringBuilder and just appending strings one by one. I think that any modern piece of &#8230; <p><a class="btn small" href="http://martinnormark.com/generate-html-e-mail-body-in-c-using-templates">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>Almost a year ago, I <a href="http://stackoverflow.com/questions/886728/generating-html-email-body-in-c/886750#886750">answered</a> a <a href="http://stackoverflow.com/questions/886728/generating-html-email-body-in-c">question</a> on <a href="http://stackoverflow.com/">StackOverflow</a>, about if there’s a <a href="http://stackoverflow.com/questions/886728/generating-html-email-body-in-c/886750#886750">better way to generate HTML e-mails in C#</a> than using a <a href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(VS.100).aspx">StringBuilder</a> and just appending strings one by one.</p>
<p>I think that any modern piece of software today, needs to send e-mail. Whether it being password recovery e-mails, rich reports, newsletters or anything else – being able to easily see and customize the look and feel of your e-mails is vital.</p>
<p>So, the worst way I could think of is having your HTML hidden away in some StringBuilder.Append() hell. The best solution (in my opinion) would be, if you could have plain old HTML files with parameters like &lt;#FirstName#&gt; so you could dynamically replace those at runtime.</p>
<h1>The MailDefinition class</h1>
<p>Luckily, there’s a class for that! If you read the comments on my answer on <a href="http://stackoverflow.com/">StackOverflow</a>, you’ll notice that none of them was familiar with that class and it came as a bit of a surprise. It did for me too, I actually wrote my own “<a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.maildefinition(VS.100).aspx">MailDefinition</a>” class that did just about the same thing. A complete waste of time, and it only tells me that us, developers, are sometimes too trigger happy on the keyboard instead of doing some research first. A quick search in the <a href="http://msdn.microsoft.com/en-us/library/ms229335(VS.100).aspx">MSDN documentation</a> would have saved me some work here and there.</p>
<p>Using the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.maildefinition(VS.100).aspx">MailDefinition</a> class is pretty straight forward. You use the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.maildefinition(VS.100).aspx">MailDefinition</a> class to create an instance of <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage(VS.100).aspx">MailMessage</a>, which you can send straightaway:</p>
<pre name="code" class="csharp">
MailDefinition md = new MailDefinition();
md.From = "test@domain.com";
md.IsBodyHtml = true;
md.Subject = "Test of MailDefinition";

ListDictionary replacements = new ListDictionary();
replacements.Add("<%Name%>", "Martin");
replacements.Add("<%Country%>", "Denmark");

string body = "<html><head></head><body>
<div>Hello <b><%Name%></b>

You're from <%Country%>.</div>

</body></html>";

MailMessage msg = md.CreateMailMessage("you@anywhere.com", replacements, body, new System.Web.UI.Control());
</pre>
<p>Instead of having the HTML inside the code, you could easily have it in your database or as a file on the computer. That will also enable you to edit the look and feel of the HTML template at any time, without having to rebuild and deploy your application.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.maildefinition(VS.100).aspx">MailDefinition</a> is located in the <strong>System.Web assembly</strong>, so don’t forget to add reference to that from your project.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/generate-html-e-mail-body-in-c-using-templates/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

