Search code examples
emailmodel-view-controllerhtml-emailurl-encoding

Update URL in Email body dynamically in mvc/c#


Working on sending the account verification email in mvc. I created a format for email body such as below and kept in html file inside solution called "EmailFormat.html".

<body>
Dear [Name],
<p>Thanks for registration.To activate your account please follow the link below:</p>
<p><a href="[url]"> click here for verification </a></p>
<p>if you are not Name or didn't register on abc.com you can ignore this email.</p>
<p>Regards,</p>
<p>www.abc.com</p>
</body>

I have a c# class handling email send.I get the above mentioned html file as email body and try to replace the [Name] and [url] with actual runtime paramaters.

 string name = myclass.FirstName + " " + myclass.LastName;
    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("abc." + "EmailFormat.html"))
    using (StreamReader reader = new StreamReader(stream))
       {
            body = reader.ReadToEnd();
        }             

     body.Replace("[Name]", name);
     string url = HttpUtility.HtmlEncode("http://localhost/Controller/Method/uniqueid")
     body.Replace("[url]",url);
     emailutility.send();//ignore this send code.

I receive email just fine but the problem is the on clicking "click here for verification", it takes me to %5Burl%5D instead of actual url.

Please help.

Thanks


Solution

  • My Bad. it was unable to update the body after body.Replace("[Name]", name); I changed it to as follows :-

    body = body.Replace("[Name]", name);
    body = body.Replace("[url]",url);
    

    Resolved!!!!