Having written no less than two introductary posts, I suppose it's high time I wrote something worthwhile to my fellow tech bloggers. This post goes out to all the ASP.NET coders reading my blog. Both of you.
If you've created more than a few websites, you probably know that sending an email is a requirement in almost every site you'll ever be asked to build. It's such a ubiquitous feature that it's even a part of the simplest practical website to actually require server side scripting: the form mailer. And while sometimes plain text emails will suffice, other times the situation calls for the prettier presentation of HTML emails.
If you're like me, you'll find that a lot of your code resembles this:
string html = "";
html += "<html>\n";
html += " <head>\n";
html += "\n";
html += " <style type=\"text/css\">\n";
html += " body,td \n";
html += " {\n";
html += " font: 12pt Arial; \n";
html += " }\n";
html += " </style>\n";
html += "\n";
html += " </head>\n";
html += "<body>\n";
html += "\n";
html += "<div id=\"thanksdiv\">\n";
html += "Thank you for your order!\n";
html += "</div>\n";
html += "\n";
html += "<div id=\"bodydiv\">\n";
html += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
html += "<tr><td>\n";
...
return html;
There are two big problems with this approach. The first is time of development: first, you have to write & test the HTML. Then, you have to convert it into code, inserting the code to append a string, escape all the embedded double quotes, and close the string. The second problem is maintenance. What happens if you need to change the email?
What would be ideal is if you could have the flexibility of using Visual Studio's HTML preview functions, while still maintaining the ability to send the email as a string. Enter an unorthodox use of UserControls.
Instead of dynamically building a string that hardcodes the body of the email, put the HTML in a UserControl, like this:
EmailBody.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EmailBody.ascx.cs" Inherits="EmailBody" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page Title</title>
<style type="text/css">
body
{
font: 12pt Arial;
color: red;
}
</style>
</head>
<body>
<h3>Thank you for your order!</h3>
<p>Your email address is <%=EmailAddress %>.</p>
</body>
</html>
EmailBody.cs:
public partial class EmailBody : System.Web.UI.UserControl
{
private string email = "";
public string EmailAddress
{
get { return email; }
set { email = value; }
}
}
To get the body of the email, rendering the UserControl to a string using this method:
public static string GetEmailBody(UserControl ctl)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter( sb );
HtmlTextWriter htw = new HtmlTextWriter( sw );
ctl.RenderControl( htw );
return sb.ToString();
}
When it's time to send the email, get the body of the desired user control like this:
EmailBody email = (EmailBody) LoadControl("~/EmailThankYou.ascx");
email.EmailAddress = "charliekilian@gmail.com";
string body = EmailManager.GetEmailBody((UserControl) email);
Note how we set the necessary properties to render the control properly.
That's it! I hope you find this tip useful!