Solved

Load HTML File Into Email Message Body

Posted on 2007-11-15
8
1,704 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

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

Andrew
0
 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

734 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