Send email.  Set body from HTML file

Posted on 2010-08-26
Last Modified: 2012-06-27
I am sending out an email very simply using the below code from within a WinApp not ASP

      System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
      message.IsBodyHtml = true;
      message.Subject = "";      
      message.From = new System.Net.Mail.MailAddress("");
      message.Body = "";      
      System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("");

I am working on something that is needed to send out emails based on templates.   So I was curious if there was a way to build the BODY of the email from an external .html file.   It would make for this to be so much nicer versus having to put a thousand lines of HTML code within the message.Body STRING.    

I have another question which is, can I setup on the HTML page I create "if the above is even possible" to accept arguments/params to send the email off as well?   An example would be that I have a block such as [USERNAME] supplied on the html page.   I would pass the current username to the HTML page, and that is what would be sent to the user.    I've seen doing this with Resource files, but I really would like to stay away from that approach otherwise I will have to accomadate some else that is off topic.
Question by:chrisryhal
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
  • 4
  • 2
LVL 42

Accepted Solution

sedgwick earned 500 total points
ID: 33531428
what i'd do is having HTML template with place holders inside, which going be replaced with actual text before sending the email.for example, check this html: #HEAD TEXT# #BODY TEXT# now, before sending the email, you find load the html file to a string variable, find replace the "#BODY TEXT#" with the body text you wish to insert to the html and assign it to can go further and have css files and expand the html template but i don't think it's necessary in your case (if only body and maybe title are needed to be changed upon each email)

Author Comment

ID: 33531691
Sounds like a heck of a plan Sedgwick, will load it up and let you know.  

Author Comment

ID: 33532915
Ok, I have it working for the most part, but I'm thinking the best way to accomadate multiple values would be a loop.   I'm kinda having a brainfart here though.
Ok, so for starters it works for the most part and I was able to use a StreamReader to fetch my HTML using System.IO

      StreamReader streamReader = new StreamReader(@"\\<myserver>\ActiveCustomerTemplate.htm");
      string strHTMLBody = streamReader.ReadToEnd();

But lets say that the HTML that is pulled is stored like so........

          string strHTMLBody = "##STR1## walked with ##STR3## to avoid being seen by ##STR2## and ##STR4## walked with ##STR5##";

and I have an array setup like so that will be the "REPLACE" variables

            string str1 = "Chris";
            string str2 = "Jack";
            string str3 = "Heather";
            string str4= "Mark";
            string str5 = "Carla";
            string[] strArray = { str1, str2, str3, str4, str5 };
            foreach (string str in strArray)

What would be the most efficient manner to replace the ##STR1## with the "string str1" since the strHTMLBody variable will have multiple replaces required?
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now


Author Comment

ID: 33532953
So I guess in terms of not being too complex, I'm trying to replace multiple values like so

         strEmailBody = strHTMLBody.Replace("##STR1##", str1);

and "strEmaiBody" would of course be the "mail.Body" value
LVL 42

Assisted Solution

sedgwick earned 500 total points
ID: 33535282
the most efficient way is to load the html file to stringbuilder and using index to run from start to finish looking for the place holders and while doing so, replacing the place holders with the actual text.
however, if you have only a few place holders than it would be easier to simply do the following:

   StreamReader streamReader = new StreamReader(@"\\<myserver>\ActiveCustomerTemplate.htm");
      StringBuilder strHTMLBody = new StringBuilder(streamReader.ReadToEnd());
string[] strArray = { str1, str2, str3, str4, str5 };
            for(int i=0;i<strArray.Length;i++))
                  strHTMLBody.Replace(string.Format("##STR{0}##", i+1),strArray[i]);

Mail.Body = strHTMLBody;


Author Comment

ID: 33558278
Thank you so much for all of your help.  

Featured Post

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!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

689 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