• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

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

0
TonyReba
Asked:
TonyReba
  • 5
1 Solution
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now