Link to home
Start Free TrialLog in
Avatar of bjeverett
bjeverettFlag for United States of America

asked on

Load HTML File Into Email Message Body

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.   :-(
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

           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
obviously you will hcnage the path to your web application path. not the local harddrive

Andrew
ASKER CERTIFIED SOLUTION
Avatar of ventaur
ventaur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ventaur: Nice Solution :-)

Andrew
Thank you!

Anything to help.


-Matt
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);
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

Okay, I'll give it a try. Thank you..