1

In my ASP.NET MVC web app, I have set up password recovery and it all works very well, however the hyperlink will not display correctly.

It prints out the entire url rather than the text I'd like to have eg. "here"

Is this an issue with my code or perhaps just the email.

 string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
 var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
 await UserManager.SendEmailAsync(user.Id, "Holiday Tracker Reset Password", "Please reset your password by using the following link. Copy and paste it into the url. <a href=\"" + callbackUrl + "\">here</a>");
 return RedirectToAction("ForgotPasswordConfirmation", "Account");

Sned Email Action:

   void sendMail(IdentityMessage message)
    {
        #region formatter
        string text = string.Format("", message.Subject, message.Body);
        string html = "";

        html += HttpUtility.HtmlEncode(@"" + message.Body);
        #endregion

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"].ToString());
        msg.To.Add(new MailAddress(message.Destination));
        msg.Subject = message.Subject;
        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

        SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com", Convert.ToInt32(587));
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Email"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
        smtpClient.Credentials = credentials;
        smtpClient.EnableSsl = true;
        smtpClient.Send(msg);
    }

Ideally, I'd like the user not have to paste the link into the url bar but rather be able to click the hyperlink

2
  • 1
    Maybe you didn't set the email body to be HTML instead of plain text? We can't see the code which actually creates the mail message Commented Apr 29, 2019 at 10:00
  • I've added the send email action to the question. But I'm unsure where to set it to HTML Commented Apr 29, 2019 at 10:20

1 Answer 1

1

It sounds like your email is being sent in plain text rather than HTML format.

You can resolve this by setting the IsBodyHtml property of your MailMessage object. In your case:

msg.IsBodyHtml = true;

This is documented here: https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.isbodyhtml?view=netframework-4.8

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.