Link to home
Start Free TrialLog in
Avatar of chrisryhal
chrisryhal

asked on

Send email. Set body from HTML file

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.To.Add("");                  
      message.Subject = "";      
      message.From = new System.Net.Mail.MailAddress("");
      message.Body = "";      
      System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("");
      smtp.Send(message);

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.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chrisryhal
chrisryhal

ASKER

Sounds like a heck of a plan Sedgwick, will load it up and let you know.  
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();
      streamReader.Close();

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)
            {
                  MessageBox.Show(str);
            }

What would be the most efficient manner to replace the ##STR1## with the "string str1" since the strHTMLBody variable will have multiple replaces required?
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much for all of your help.