我的string.replace()不能在我的类文件中工作 [英] My string.replace() won't work in my class file
问题描述
最近我在创建一个HTML格式的电子邮件给客户预订信息的任务。
我之前在页面返回代码中做过,但现在我需要使用一个没有从默认页面继承的类文件来创建这个html格式的电子邮件。
然后,我发现我的string.replace()不会在我的班级文件中工作。
我创建了一个项目,在我的数据文件中,我格式化了我的电子邮件,我使用的所有动态信息## information ##持有它。
然后我用string.replace(oldstring,newstring)替换## information ##这个占位符,但它发送我的电子邮件,但不会替换数据。
只是为了确保它能够正常工作然后我才能真正发挥作用生活项目,所以在附件中我的测试项目你不介意看看为什么string.replace()不起作用吗?
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?
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);
编辑(Matt T Heffron):更改为虚拟电子邮件地址以保护垃圾邮件。
EDIT (Matt T Heffron): Changed to dummy email address for spam protection.
推荐答案
.NET中的字符串是不可变的。你需要保留从Replace方法返回的字符串。
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:");
.NET中的字符串是不可变的!
string.Replace()
不修改字符串。它返回一个带有替换的新字符串。
尝试:
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:
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
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");
Hi Matt T Heffron,
它的确有效,非常感谢。你救了我的命。
很好理解string.Replace()现在如何工作。
Hi Matt T Heffron,
It works, Thanks very much. You saved my life.
Very good that understand how string.Replace()work now.
这篇关于我的string.replace()不能在我的类文件中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!