Solved

Send email.  Set body from HTML file

Posted on 2010-08-26
6
611 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now