Solved

Load HTML File Into Email Message Body

Posted on 2007-11-15
8
1,705 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

630 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