String replacement in html email body ASP.NET

I am sending a form information in an asp.net email loading htm as string and replacing tokens. The way I'm thinking to do it is as follows but the problem is that my Body is not getting the value from the textbox and I get {0}  on the email

Can you please point out whats wrong?
Thanks in advance!
// Change to false to not include HTML 

            message.IsBodyHtml = true;
            string bodyHTML = string.Empty;
            string bodyPlain = string.Empty;

            var body = string.Format(bodyHTML, HttpUtility.HtmlEncode(txtFirstName.Text), HttpUtility.HtmlEncode(txtLastName.Text)); 

            
      

            bodyHTML = @"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
'http://www.w3.org/TR/html4/loose.dtd'>
<html lang='en'>
<head>
</head>
<body>
<table style='width:800px; border:1px solid #000;' cellpadding='10' cellspacing='0' align='center'>
<tr>
<td colspan='2' style='background-color:#009797; border-bottom:1px solid #000;'>
<h1 style='text-align:center; color:#FFF;'>Pre-Registration Information</h1></td>
</tr>
  <td style='width:600px; vertical-align:top;'>
<h4 style='font-family:Arial, Helvetica, sans-serif; color:#000;'><u>PATIENT INFORMATION</u></h4>
<p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>Last Name:{0}</p>

<p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>First Name:{1}</p>

</table>
</body>
</html>'";

Open in new window

LVL 9
TonyRebaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GreggCommented:
Please notice you are calling string.Format() method before populating the bodyHTML variable. Its null.
0
GreggCommented:
TonyReba, you should also consider using StreamReader to load the html email into a variable from a text file. This way you dont have to use so much string concatenation. Much cleaner and faster.

http://www.csharp-examples.net/load-text-file-to-string/

0
TonyRebaAuthor Commented:
not working please see full code
if ((Page.IsValid))
        {
            if (dpAction.SelectedValue.ToString()=="Submit" )
            {
            // define SMTP client

            SmtpClient smtp = new SmtpClient("mystmp");
            //smtp.EnableSsl = true; 
            // smtp.UseDefaultCredentials = true;
            //create the mail message
            MailMessage message = new MailMessage();

            //From address will be given as a MailAddress Object
            message.From = new MailAddress("myemail@dhr-rgv.com");

           
            //To address collection of MailAddress
            message.To.Add("ric.gutierrez@rgv.com");
            message.Subject = "Pre-Registration Form";

            //CC and BCC optional
            // message.CC.Add("ric.gutierrez@dhr-rgv.com");

            // Change to false to not include HTML 

            message.IsBodyHtml = true;
            string bodyHTML = string.Empty;
            string bodyPlain = string.Empty;

            bodyHTML = string.Format(bodyHTML, HttpUtility.HtmlEncode(txtFirstName.Text), HttpUtility.HtmlEncode(txtLastName.Text));               
            bodyHTML = @"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
            'http://www.w3.org/TR/html4/loose.dtd'>
            <html lang='en'>
            <head>
            </head>
            <body>
            <table style='width:800px; border:1px solid #000;' cellpadding='10' cellspacing='0' align='center'>
            <tr>
            <td colspan='2' style='background-color:#009797; border-bottom:1px solid #000;'>
            <h1 style='text-align:center; color:#FFF;'>Pre-Registration Information</h1></td>
            </tr>
              <td style='width:600px; vertical-align:top;'>
            <h4 style='font-family:Arial, Helvetica, sans-serif; color:#000;'><u>PATIENT INFORMATION</u></h4>
            <p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>Last Name:{0}</p>

            <p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>First Name:{1}</p>
            <p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>Middle Name:</p>
            <h4 style='font-family:Arial, Helvetica, sans-serif; color:#000;'><u>SERVICE/PROCEDURE INFORMATION</u></h4>
            <p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>Procedure/Test:</p>
            <p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>Date of Procedure:</p>
            <h4 style='font-family:Arial, Helvetica, sans-serif;color:#000;'><u>INFORMATION SUBMITTED ON:</u></h4>
            <p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>Date/Time of Data submitted:</p>
            <p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>Confirmation Number:</p>
              </td>
            </tr>
            <tr>
            <td colspan='2' style='background-color:#009797; color:#000; font-family:Arial, Helvetica, sans-serif; font-size:.8em; border-top:1px solid #000;'>Doctors Hospital at renaissance.</td>
            </tr>
            </table>
            </body>
            </html>'";

            var body = string.Format(bodyHTML, HttpUtility.HtmlEncode(txtFirstName.Text), HttpUtility.HtmlEncode(txtLastName.Text));

            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(bodyHTML, null, "text/html");
            message.AlternateViews.Add(htmlView);


            message.Body += "<b>PATIENT INFORMATION</b> </br></br>" + txtLastName.Text + " <br /> First Name" + txtFirstName.Text +
            "Middle Name:";
           /* message.Body += "<b>Preferred contact method: </b> " + drp_contact_method.SelectedValue + "<br/>";
            message.Body += "<b>Phone Number: </b> " + txtPhone.Text + " <br />";
            message.Body += "<b>Comments: </b>" + txtMessage.Text + " <br /><br />";

            */

            //message.Body += "<b>To review/reply to this message please go to : </b> <br />"

            //send the message

            try
            
            {
                smtp.Send(message);
            
            }
            catch (Exception ex) { Console.Write(ex.Message); } 
           // smtp.Send(message);

            //Redirect to confirmation page
            Response.Redirect("Confirmation.aspx");

            }
        }

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GreggCommented:
Hi,

2 things to look at:

1. Comment out the line of code located above your html body:
bodyHTML = string.Format(bodyHTML, HttpUtility.HtmlEncode(txtFirstName.Text), HttpUtility.HtmlEncode(txtLastName.Text));  

2. Correct the string concatenation for your html body. See blog post: Multi-Line Strings in C#
0
GreggCommented:
Sorry TonyReba!, I misintrepreted the blog post. Ignore comment #2.
0
GreggCommented:
Is c# case sensitive like java is?

String.Format("The {0} jumped over the {1}.", "cow", "moon");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.