Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2649
  • Last Modified:

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 ?
0
mugsey
Asked:
mugsey
  • 6
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now