1

Good morning all,

I am currently trying to send an email after a user fills in a form. The email contains a link to an activation page. My code for generating the link is as follows:

mailMessage.Body = "<h1>Please click the link below to activate your account</h1><br /><a href = '" + HttpContext.Current.Request.Url.AbsoluteUri.Replace("Activation.aspx", "Activation.aspx?userId=" + userId) + "'>Click here to activate your account.</a>";

However the url the email shows when sent is:

http://localhost:52621/RegisterUser.aspx

What am I doing wrong? As shown in the code that generates the link I am trying to get to a page by the name of Activation.aspx which is not happening. Any suggestions would be great. Thanks in advance.

M_Griffiths
  • 547
  • 1
  • 7
  • 26
  • Whats the content of AbsoluteUri? – Romano Zumbé Jun 01 '17 at 06:27
  • You're clearly getting the Uri from RegisterUser.aspx page. Also, you might want to append the "?userId=..." part to your AbsoluteUri, instead of replacing part of it, because the Uri will (after you retrieve it) probably end with "Activation.aspx" anyway. – Piotr Nowak Jun 01 '17 at 06:32
  • https://stackoverflow.com/questions/10719722/asp-net-app-to-send-an-mail-with-a-hyperlink – Ramesh Kumar Jun 01 '17 at 13:20
  • Here's an inline link to [Google](https://stackoverflow.com/questions/10719722/asp-net-app-to-send-an-mail-with-a-hyperlink/). – Ramesh Kumar Jun 01 '17 at 13:23

1 Answers1

0

You are probally running a local development server at port 52621, that is why you are getting the above URL.

When you publish your project it will look differently, probally runnning at some domain like

https://www.example.com/RegisterUser.aspx

If you want to have the link from the published state one solution is to code it manually:

mailMessage.Body = "<h1>Please click the link below to activate your account</h1><br /><a href='https://www.example.com/Activation.aspx?userId=" + userId + "'>Click here to activate your account.</a>";

I do not like that one, though.

If you have multiple environments that your project will be published to you could write their domains into the web.config file depending on the current build configuration like described here:

Using different Web.config in development and production environment

Cossintan
  • 114
  • 3
  • 12
  • Thank you for your answer. In the end I went with a different verification method. I have accepted this as the answer anyway as you gave me a couple of things to think about. Thank you – M_Griffiths Jun 02 '17 at 10:47