Solved

Load HTML File Into Email Message Body

Posted on 2007-11-15
8
1,697 Views
Last Modified: 2008-02-01
If I have an HTML page located in my ASP.Net app, is there a way to just load the HTML from that file into the body of an email message?

Right now I'm building an HTML string for the message which is a little tedious.   :-(
0
Comment
Question by:bjeverett
  • 3
  • 3
  • 2
8 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20289532
           using (System.IO.StreamReader sr1 = new System.IO.StreamReader(@"C:\TestFile.htm"))
            {
                string contents = sr1.ReadToEnd();
            }

That will get you the contents of the file :-)

Andrew
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20289536
obviously you will hcnage the path to your web application path. not the local harddrive

Andrew
0
 
LVL 6

Accepted Solution

by:
ventaur earned 50 total points
ID: 20289743
I often use HTML templates for my emails. You can add tokens to your HTML file as placeholders for dynamic content; sort of like a mail merge. So, if you have some HTML with tokens like the following, you can use the attached code to replace them with real data before emailing:

...
<p>Dear [FirstName]:<br /><br />
We are happy to welcome you to our online community. Feel free to browse our experts' solutions and stay informed on the technology you care about most.</p>
<p>Your new membership is effective: [MembershipDate].</p>
...
using System.IO;

...
 

// Read the HTML template from file.

string EmailBody = File.ReadAllText(PathToHtmlFile);
 

// Replace tokens with real values.

string EmailBody = EmailBody.Replace("[FirstName]", UserFirstName);

string EmailBody = EmailBody.Replace("[MembershipDate]", UserMembershipDate.ToString("MMM dd, yyyy"));
 

// Send email using the EmailBody.

...

Open in new window

0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20289791
ventaur: Nice Solution :-)

Andrew
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Expert Comment

by:ventaur
ID: 20289883
Thank you!

Anything to help.


-Matt
0
 

Expert Comment

by:pointeman
ID: 23606260
Automate TextBox Form Mail:
I almost have this figured out, but theres a snag. Unsure how to get TextBox.Text as you'll see below:
[This works]
string emailer = File.ReadAllText(Server.MapPath("~/emailtempl.htm"));

emailer = emailer.Replace("[txtFullName]", txtFullName.Text);
emailer = emailer.Replace("[txtEmailAddr]", txtEmailAddr.Text);
System.IO.File.WriteAllText(Server.MapPath("~/emailer.htm"), emailer);
 [This does NOT work]
string emailer = File.ReadAllText(Server.MapPath("~/emailtempl.htm"));
for (int i = 0; i < 10; i++)
{
    string txtPattern = Regex.Match(emailer, @"(?<=\[)txt(.*?)(?=\])", RegexOptions.IgnoreCase).ToString(); //ie... matches [txtFullName]
    emailer = emailer.Replace("[" + txtPattern + "]", ((TextBox)(FindControl("txtPattern"))).Text); // error: object not set to the instance...
}
System.IO.File.WriteAllText(Server.MapPath("~/applyEmailer.htm"), emailer);
0
 
LVL 6

Expert Comment

by:ventaur
ID: 23612399
Try something like this to suit your needs. By using a MatchEvaluator, you can customize the retrieval of your replacement string in a nice method that uses the matched token value to find your text boxes.

I use this technique to replace tokens with data from the following: IDataReader, DataSet, DataTable, Hashtable, the property on an Object, etc.
private string EvaluateMatchForTextBoxInput(Match match) {

	string Result = null;
 

	// Find the matched item on the form.

	TextBox Input = FindControl(match.Groups[1].Value) as TextBox;

	if (Input != null)

		Result = Input.Text;

	else

		Result = "[ missing data ]";
 

	return Result;

}
 

...

string EmailBody = File.ReadAllText(Server.MapPath("~/emailtempl.htm"));

EmailBody = Regex.Replace(EmailBody, @"\[(\w+)\]", new MatchEvaluator(EvaluateMatchForTextBoxInput), RegexOptions.IgnoreCase | RegexOptions.Multiline);

...

Open in new window

0
 

Expert Comment

by:pointeman
ID: 23613622
Okay, I'll give it a try. Thank you..
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

760 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

18 Experts available now in Live!

Get 1:1 Help Now