?
Solved

Load HTML File Into Email Message Body

Posted on 2007-11-15
8
Medium Priority
?
1,706 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 200 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

719 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