emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
// now that the emailBody variable is set, send the email
emailer.sendEmail(emailAddress, Master.noReplyEmail, emailSubj, emailBody);
emailBody
is a non-null string variable that contains the body of the email I'开发者_开发技巧m going to send out, including the phrase "[CONFIRMATION_LINK]". I want to replace that phrase with the content of confirmLink
variable (some URL).
When I send my email, I still see the phrase "[CONFIRMATION_LINK]" in the email body. Why?
Strings are immutable. String operation generally return new string instances. Try this:
emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
The Replace
method returns a new, updated string. You need to assign the results of the Replace
call back to emailBody
:
emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
Strings in C# are immutable, meaning they cannot be changed once they are instantiated unless explicitly pointed to another string. What happens is that you are calling "Replace" without any variable to receive the string that returns.
Change the 2nd to the last line with this code:
emailBody = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
It's because Replace does not edit the emailBody
variable. It returns the resultant string. In this case, you'd need:
string bodyToSend = emailBody.Replace("[CONFIRMATION_LINK]", confirmLink);
emailer.sendEmail(emailAddress, Master.noReplyEmail, emailSubj, bodyToSend);
精彩评论