Solved

Load HTML File Into Email Message Body

Posted on 2007-11-15
8
1,699 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Class object 2 26
Two different visual studio versions 3 21
Turn on intranet settings 1 36
Form Submit falis 6 25
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

11 Experts available now in Live!

Get 1:1 Help Now