Solved

asp.net add html templates to email

Posted on 2008-10-28
8
2,588 Views
Last Modified: 2013-12-17
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 ?
0
Comment
Question by:mugsey
  • 6
  • 2
8 Comments
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22824023
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
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22824043
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
 

Author Comment

by:mugsey
ID: 22824064
OK what is best way to present to user list of html files to choose
0
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 22824320
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 13

Expert Comment

by:numberkruncher
ID: 22824371
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
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22824383
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
 

Author Comment

by:mugsey
ID: 22824637
ok thank you friend, very good comments.

What if I save HTML in database?  Is this good?
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22825006
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now