Solved

Send email.  Set body from HTML file

Posted on 2010-08-26
6
613 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

11 Experts available now in Live!

Get 1:1 Help Now