Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
In recently I work on the task to create a HTML format email to client the booking information.

I did it before in page back code,but now I need use a class file that didn't inherit from default page to create this html format email.

Then, I found that my string.replace()won't work in my class file.


I create a project and in my Data file I format my email, all the dynamic information I use ##information## to holder it.

Then I use string.replace(oldstring,newstring) to replace "##information##" this placeholder, but it send my email, but won't replace the Data.


Just for make sure it work then I going to put on my real life project, so in the attachment is my test project won't you mind see why the string.replace()won't work?

C#
public static void GenerateEmails()
        {

            string body_confirmation;

            // For customer confirmation
            using (StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/Data/EmailCustomerConfirm.htm")))
            {
                body_confirmation = reader.ReadToEnd();
            }
            //System.Web.HttpContext.Current.Response.Write(body_confirmation);
            body_confirmation.Replace("##comfirmStates##", "You have submitted the following booking:");
            body_confirmation.Replace("##Customer##", "zhangdongling@hotmail.com");
            body_confirmation.Replace("##Job No#", "Job No");
            body_confirmation.Replace("##Reference##", "Reference");
            body_confirmation.Replace("##PassengerName##", "Sandy");
            body_confirmation.Replace("##PassengerNumber##", "Sandy passenger Number");
            body_confirmation.Replace("##Contact Phone##", "contact phone");
            MailMessage myMessage = new MailMessage();
            myMessage.Subject = "Response from web site";
            myMessage.Body = body_confirmation;
            myMessage.IsBodyHtml = true;
            myMessage.From = new MailAddress("12345@nowhere.com", "web site");
            myMessage.To.Add(new MailAddress("12345@nowhere.com", "sandy zhang"));
            var mySmtpClient = new SmtpClient();
            mySmtpClient.Send(myMessage);


EDIT (Matt T Heffron): Changed to dummy email address for spam protection.
Posted
Updated 5-May-14 15:03pm
v3

String in .NET is immutable. You need to keep the returned string from Replace method.

body_confirmation = body_confirmation.Replace("##comfirmStates##", "You have submitted the following booking:");
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-May-14 20:55pm    
Sure, a 5.
—SA
Matt T Heffron 5-May-14 20:57pm    
+5 (you typed a bit faster than I did...)
strings in .NET are immutable!
string.Replace() does not modify the string in place. It returns a new string with the replacement(s) performed.
Try:
C#
body_confirmation = body_confirmation.Replace("##comfirmStates##", "You have submitted the following booking:");
body_confirmation = body_confirmation.Replace("##Customer##", "zhangdongling@hotmail.com");
body_confirmation = body_confirmation.Replace("##Job No#", "Job No");
body_confirmation = body_confirmation.Replace("##Reference##", "Reference");
body_confirmation = body_confirmation.Replace("##PassengerName##", "Sandy");
body_confirmation = body_confirmation.Replace("##PassengerNumber##", "Sandy passenger Number");
body_confirmation = body_confirmation.Replace("##Contact Phone##", "contact phone");

OR
C#
body_confirmation = body_confirmation.Replace("##comfirmStates##", "You have submitted the following booking:")
          .Replace("##Customer##", "zhangdongling@hotmail.com")
          .Replace("##Job No#", "Job No")
          .Replace("##Reference##", "Reference")
          .Replace("##PassengerName##", "Sandy")
          .Replace("##PassengerNumber##", "Sandy passenger Number")
          .Replace("##Contact Phone##", "contact phone");
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-May-14 21:00pm    
Of course, a 5.
—SA
Shao Voon Wong 5-May-14 21:44pm    
2 complete answers! 5 from me!
Santosh K. Tripathi 6-May-14 0:16am    
+5 from me
Hi Matt T Heffron,

It works, Thanks very much. You saved my life.


Very good that understand how string.Replace()work now.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900