<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Brendan Tompkins : Resources, Featured</title><link>http://devlicio.us/blogs/brendantompkins/archive/tags/Resources/Featured/default.aspx</link><description>Tags: Resources, Featured</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Using Embedded Resources for HTML Email Templates</title><link>http://devlicio.us/blogs/brendantompkins/archive/2007/10/24/using-embedded-resources-for-html-email-templates.aspx</link><pubDate>Wed, 24 Oct 2007 15:23:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:38712</guid><dc:creator>Brendan Tompkins</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/brendantompkins/rsscomments.aspx?PostID=38712</wfw:commentRss><comments>http://devlicio.us/blogs/brendantompkins/archive/2007/10/24/using-embedded-resources-for-html-email-templates.aspx#comments</comments><description>
&lt;p&gt;Most web applications need to send out email for a variety of purposes, right?&amp;nbsp;&amp;nbsp; I like to use email as a sort of extended user interface - allowing entry points to let the user conveniently do work by getting an email and clicking on a link.&amp;nbsp; I&amp;#39;ve struggled for a while with a good way of managing email templates to use in my applications.&amp;nbsp; I recently found a good project that simplifies the process of templating - See &lt;a href="http://www.codeproject.com/script/Articles/list_articles.asp?userid=2247710"&gt;Alexander Kleshchevnikov&amp;#39;s&lt;/a&gt; excellent solution for&lt;span id="intelliTXT"&gt; &lt;a href="http://www.codeproject.com/useritems/mailtemplates.asp"&gt;managing and sending emails inside a web application&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;I like his template approach, but I don&amp;#39;t like to keep my template files in a directory with my web project, mainly because it tightly couples these files and a certain directory structure to the classes that actually do the email work.&amp;nbsp; This makes my classes harder to test, re-use and deploy.&amp;nbsp; I recently figured out how to embed these templates into my class&amp;#39; assembly files, making the class much more useful.&amp;nbsp; &lt;/p&gt;

&lt;p&gt;To embed a resource, just drop it in your project, and select &amp;quot;Embedded Resource&amp;quot;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://devlicio.us/photos/brendan/images/38713/original.aspx" height="387" width="500" alt="" /&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Using Alexander&amp;#39;s solution for templating, and an embedded resource html file for a template, the code to use this resource looks like this:&lt;br /&gt;&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;        public static void SendRegisterNotification(User user)
        {
            Hashtable templateVars = new Hashtable();
            templateVars.Add(&amp;quot;FirstName&amp;quot;, user.FirstName);
            templateVars.Add(&amp;quot;LastName&amp;quot;, user.LastName);
            templateVars.Add(&amp;quot;Login&amp;quot;, user.UserId);
            templateVars.Add(&amp;quot;Password&amp;quot;, user.Password);

            Parser parser = new Parser(templateVars);
            Assembly asm = Assembly.GetExecutingAssembly();
            Stream stream = asm.GetManifestResourceStream(asm.GetName().Name +
                &amp;quot;.Registration.htm&amp;quot;);

            using (StreamReader reader = new StreamReader(stream))
            {
                parser.TemplateBlock  = reader.ReadToEnd();
            }

            // Send email
            System.Net.Mail.MailMessage message = 
                  new System.Net.Mail.MailMessage();
            message.To.Add(user.Email);
            message.From = new MailAddress(&amp;quot;brendan.tompkins@gmail.com&amp;quot;);
            message.Subject = &amp;quot;Your StreetTurns.Com Account Information&amp;quot;;
            message.Body = parser.Parse();
            message.IsBodyHtml = true;
            SmtpClient sc = new SmtpClient(&amp;quot;StreetTurns.Com&amp;quot;);
            sc.Send(message);
        }
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Yes, but what about Modifying your Templates?&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;As &lt;a href="http://codebetter.com/blogs/jeremy.miller/default.aspx"&gt;Jeremy Miller&lt;/a&gt; has pointed out, you shouldn&amp;#39;t be afraid to compile and release your code.&amp;nbsp; If you are, you need some serious work on your build process.&amp;nbsp; To me, the benefit of having one class that I can drop anywhere without worrying about permissions, file paths, and all that far outweighs the need to re-build the DLL when I make a text change.&lt;/p&gt;

&lt;p&gt;I&amp;#39;m pretty happy about the way this all works.&amp;nbsp; For more on embedded resources, ther&amp;#39;s some information &lt;a href="http://aspalliance.com/726"&gt;here&lt;/a&gt; and &lt;a href="http://aspnet.4guysfromrolla.com/articles/080906-1.aspx"&gt;here&lt;/a&gt; about how you can access your resources via a URL in a web app.&lt;br /&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=38712" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/brendantompkins/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://devlicio.us/blogs/brendantompkins/archive/tags/Resources/default.aspx">Resources</category><category domain="http://devlicio.us/blogs/brendantompkins/archive/tags/Featured/default.aspx">Featured</category></item></channel></rss>