Solved

Send email.  Set body from HTML file

Posted on 2010-08-26
6
612 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET HttpWebRequest 12 58
What's the most efficient way to load up a couple of large table in C#? 5 39
Class object 2 28
Close form "before" open 3 30
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

20 Experts available now in Live!

Get 1:1 Help Now