Reputation: 577
I have create a mail template ,but some value will be dynamic from cs file , I try to using ##username in my mail template , and using mailbody.Replace("##UserName", "jack"); to replace the username value in mail template ,but it's still show ##UserName in my mail, please how to replace some content in mail template before send in C# ?
my mail template : mailtemplate.html :
<div>
<table>
<tr>
<td> ##UserName<span style='font-size:24px'><span></td>
</tr>
</table>
</div>
</div>
my sendmail.aspx.cs :
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
string mailbody = string.Empty;
using (StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath("mailtemplate.html")))
{
mailbody = reader.ReadToEnd();
}
mailbody.Replace("##UserName", "jack");
mm.Body = mailbody;
Upvotes: 0
Views: 2949
Reputation: 710
You are just using your "original" mailbody.
You need to do sth like this
mailbody = mailbody.Replace("##UserName", "jack");
Then mailbody
will have the replaced text....if not you are just replacing, but throwing away the result...
In C# strings are inmutable, so you get a NEW one, with desired replacement
So this
mailbody = mailbody.Replace("##UserName", "jack");
mm.Body = mailbody;
or this
var replacedBody = mailbody.Replace("##UserName", "jack");
mm.Body = replacedBody;
or this
mm.Body = mailbody.Replace("##UserName", "jack");;
should work. You just need to work with the returned value of Replace
not with the original string (what you are doing)
Take a look here at the docs. Replace returns a string
, which is the result string you are looking for
Upvotes: 2