HTML Email Question

A

akylitis

Hello:

Anyone have an opinion on the best way to formulate a dynamic HTML
email? Thus far I have figured out that the only way to "build" the
HTML prior to the email (I'm using System.Net.Mail) is to manually
build the HTML string in code or "screen scrape" a stream from a
WebRequest.

I've tried both ways and I'm torn. I hate building HTML through
strings; I'd rather create and manage an HTML file. However, the final
HTML needs to be dynamic enough to display data collected from a
"Contact Us" form. At the point of the email, the data is already
posted to the DB but I'm not sure if I want to hit the DB again if the
data is already part of the business object I already have created.

I think what I'm really looking for is a way to use an html template
somehow and "place" the data in the template, set the value to the
mail.body and be done. Any thoughts?

Tony
 
M

Mark Harris

Tony,

You could store the HTML file on the server as say "emailtemplate.html"
and put some unique code where you want to replace sections with the form
variables, eg:

[--------------------------------------------------------------------------------------]
<html>

<body>

<strong>First Name: </strong> {!FIRSTNAME!}<br />
<strong>Surname: </strong> {!SURNAME!}<br />
<strong>Comments: </strong> {!COMMENTS!}<br />

</body>

</html>
[--------------------------------------------------------------------------------------]


Then:
[--------------------------------------------------------------------------------------]

// need to add using System.IO;


// open the file
StreamReader sr = new StreamReader("emailtemplate.html");
string template = sr.ReadToEnd();
sr.Close();


// replace the template fields
template.Replace("{!FIRSTNAME!}", _firstName);
template.Replace("{!SURNAME!}", _surname);
template.Replace("{!COMMENTS!}", _comments);

// set mail.body to template.


[--------------------------------------------------------------------------------------]
 
A

akylitis

Thank you Mark. I think this will work wonderfully. However, now I'm
considering making the HTML page an aspx page with data controls that
will look up the contact information from the DB. Therefore I don't
have to worry about the place holders. I think that would be best.
Then I can make that page serve multiple purposes.

Thanks again,
Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top