Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Brendan Tompkins

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 License.


Using Embedded Resources for HTML Email Templates

Most web applications need to send out email for a variety of purposes, right?   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.  I've struggled for a while with a good way of managing email templates to use in my applications.  I recently found a good project that simplifies the process of templating - See Alexander Kleshchevnikov's excellent solution for managing and sending emails inside a web application.

I like his template approach, but I don'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.  This makes my classes harder to test, re-use and deploy.  I recently figured out how to embed these templates into my class' assembly files, making the class much more useful. 

To embed a resource, just drop it in your project, and select "Embedded Resource" 


Using Alexander's solution for templating, and an embedded resource html file for a template, the code to use this resource looks like this:

        public static void SendRegisterNotification(User user)
        {
            Hashtable templateVars = new Hashtable();
            templateVars.Add("FirstName", user.FirstName);
            templateVars.Add("LastName", user.LastName);
            templateVars.Add("Login", user.UserId);
            templateVars.Add("Password", user.Password);

            Parser parser = new Parser(templateVars);
            Assembly asm = Assembly.GetExecutingAssembly();
            Stream stream = asm.GetManifestResourceStream(asm.GetName().Name +
                ".Registration.htm");

            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("brendan.tompkins@gmail.com");
            message.Subject = "Your StreetTurns.Com Account Information";
            message.Body = parser.Parse();
            message.IsBodyHtml = true;
            SmtpClient sc = new SmtpClient("StreetTurns.Com");
            sc.Send(message);
        }

Yes, but what about Modifying your Templates?

As Jeremy Miller has pointed out, you shouldn't be afraid to compile and release your code.  If you are, you need some serious work on your build process.  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.

I'm pretty happy about the way this all works.  For more on embedded resources, ther's some information here and here about how you can access your resources via a URL in a web app.



Comments

Dave Laribee said:

Cool.

What did you use for the nice code-formatting?

# October 24, 2007 1:37 PM

Brendan Tompkins said:

Dave, I have syntaxhighlighter installed on both Devlicio.us and CodeBetter, so you can use it too!

code.google.com/.../syntaxhighlighter

# October 24, 2007 1:49 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 25, 2007 12:25 PM

yves said:

Isn't it better to have you template driven from a databse inseatd of having them inside of a DLL.

I can see a great purpose of embeding certain files with data that should be secure...

but an hmtl template that every time you need to modify you have to rebuild your entrie dll and push it to the server does not seem like good seniro in a real world application.

# October 26, 2007 1:12 PM

Brendan Tompkins said:

Yves,

No, i don't think it is a better idea to have them in the database.  They become extremely hard to manage once they're there.  When you have a file that you can deploy within a dll, you can use all the tools you'd normally use to edit these files..

Again, if you're reluctant to build and deploy your code, you have a code smell in your build process!

# October 26, 2007 1:18 PM

yves said:

Ok but the Same interface to update that embedded file would be set up to update the one in your database.

As far as pushing DLL you are taking the risk to bring the app down if you have a user accessing your app whihle your DLL push. where databse can be accessed and modifed withou interuption.

# October 26, 2007 4:41 PM

Brendan Tompkins said:

I'm not sure what you mean.  I'm talking about editing the HTML file with Dreamweaver or Visual Studio.  You know an easy way to open and modify a db object?

Don't be afraid to build and deploy!  You have one dll to drop. You shouldn't have to re-start the appdomain, and if you do, and it's a web app, you just use your content switch to migrate users to your second server while you update.

# October 26, 2007 4:51 PM

Simone Busoli said:

NVelocity automatically handles embedded resources and lets you parse them with a single line of code. Plus, it has a much richer syntax. http://www.codeproject.com/useritems/nvelocityaspnet.asp
# October 26, 2007 6:37 PM

Brendan Tompkins said:

Simone,

That's very cool!

# October 26, 2007 8:13 PM

Lexapro. said:

Lexapro side effects. Lexapro.

# July 26, 2008 5:29 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Red-Gate!