asp.net add html templates to email

I want to have a set of email templates in an asp.net web app.  The html will either be stored in the app or in database.  I want user to be able to select a template and it gets placed in text box for email sending.

How can I do this ?
mugseyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

numberkruncherCommented:
You can open an externally stored file and read it in its entirety to be placed within your text box:

try
{
    StreamReader reader = new StreamReader("c:\\ee.txt");
    textBox1.Text = reader.ReadToEnd();
    reader.Close();
}
catch (Exception)
{
    // Error handling here!
}

Open in new window

0
numberkruncherCommented:
BTW "c:\\ee.txt" could be replaced with:
string filePath = Server.MapPath("~/email/templates/template1.html")
StreamReader reader = new StreamReader(filePath);
 
// Same as before...

Open in new window

0
mugseyAuthor Commented:
OK what is best way to present to user list of html files to choose
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

numberkruncherCommented:
Personally I would present the list in table form with some sort of preview image.

You can acquire a list of available template files using 'Directory.GetFiles'. Alternatively you could use an XML file to store additional information about templates (refer to source for an example).

If you choose the XML option then you could use XSL to transform the list into HTML using ASP.NET. I have provided an example of this also in the source. I like the XML route because it allows you to add descriptions, and thumbnails which the standard file listing doesn't.

What goes where!?

Website Root/                         DIR
   > CreateEmail.aspx
   > SelectTemplate.aspx
   > email-templates.xml
   > email-templates.xsl
   > email/                                DIR
           > templates/                 DIR
                 formal.html
                 formal_thumb.jpg
                 informal.html
                 informal_thumb.jpg
<!-- To use in ASP.NET -->
<asp:Xml runat="server" DocumentSource="~/email-templates.xml" TransformSource="~/email-templates.xsl" />
 
 
File: email-templates.xsl
-------------------------
<?xml version='1.0' encoding='ISO-8859-1'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="http://www.w3.org/TR/html4/strict.dtd" doctype-public="-//W3C//DTD HTML 4.01//EN" />
 
<xsl:template match="/">
	<table cellpadding="3" cellspacing="3">
		<xsl:apply-templates />
	</table>
</xsl:template>
 
<xsl:template match="template">
	<tr>
		<td width="0px">
			<img width="150px" height="150px">
				<xsl:attribute name="src">
					<xsl:value-of select="@path" />
				</xsl:attribute>
			</img>
		</td>
		<td width="100%" valign="top">
			<div style="font-weight: bold">
				<xsl:value-of select="name" />
			</div>
			<div>
				<xsl:value-of select="description" />
			</div>
			<div>
				<a>
					<xsl:attribute name="href">
						<xsl:value-of select="concat('CreateEmail.aspx?template=', @path)" />
					</xsl:attribute>
					<br />
					<xsl:text>Use this template</xsl:text>
				</a>
			</div>
		</td>
	</tr>
</xsl:template>
 
</xsl:stylesheet>
 
 
File: email-templates.xml (Your Data)
-------------------------------------
<?xml version="1.0"?>
<email-templates>
   <template path="email/templates/formal.html">
      <name>Formal Notification</name>
      <description>Use this template for sending formal messages........</description>
      <thumbnail>email/templates/formal_thumb.jpg</thumbnail>
   </template>
   <template path="email/templates/informal.html">
      <name>Informal Notification</name>
      <description>Use this template for sending informal messages........</description>
      <thumbnail>email/templates/informal_thumb.jpg</thumbnail>
   </template>
</email-templates>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
numberkruncherCommented:
The simplest option is to "hard-code" a list of templates (as shown below).


   
     
         
     
     
           Informal Template
           My description...
           
           Use this template
     
   


The query paramater on the 'CreateEmail.aspx' page is access by the CreateEmail page as follows:
Page_Load:
----------
 
string requestTemplate = Request.QueryParams["template"] ?? "templates/default.html";

Open in new window

0
numberkruncherCommented:
Sorry I will repost the above source listing, some of it has become actual HTML!
<table>
   <tr>
      <td width="0px">
          <img src="myPreview.jpg" />
      </td>
      <td width="100%">
           <b>Informal Template</b>
           <div>My description...</div>
           <a href="CreateEmail.aspx?template=templates/formal.html">Use this template">Use this template</a>
      </td>
   </tr>
</table>

Open in new window

0
mugseyAuthor Commented:
ok thank you friend, very good comments.

What if I save HTML in database?  Is this good?
0
numberkruncherCommented:
For this kind of HTML, it would not be a personal preference to place it within a database because it doesn't really relate to other data entries. If users had unique HTML content associated with them then it might more worthy of placing within a database, but again personally I prefer to have separate files.

   > Large amounts of content can slow your database down if it isn't done correctly.
   > It is more difficult to access resources for which the HTML is dependant upon. (i.e. pictures)
   > More difficult to manipulate your HTML, any changes would require you to update your database.
   > If your database becomes corrupt (and you failed to back it up frequently) you loose all of your data PLUS all of your HTML.

Also, when the HTML is stored externally you can use local references to pictures, as opposed to long absolute addressed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.