?
Solved

asp.net add html templates to email

Posted on 2008-10-28
8
Medium Priority
?
2,622 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 13

Accepted Solution

by:
numberkruncher earned 2000 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
 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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