Solved

Send email.  Set body from HTML file

Posted on 2010-08-26
6
614 Views
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.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.
0
Comment
Question by:chrisryhal
  • 4
  • 2
6 Comments
 
LVL 42

Accepted Solution

by:
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 message.Body.you 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)
0
 
LVL 2

Author Comment

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

Author Comment

by:chrisryhal
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();
      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?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:chrisryhal
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
0
 
LVL 42

Assisted Solution

by:sedgwick
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());
      streamReader.Close();
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;

0
 
LVL 2

Author Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 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