I have a register page that sends activation link to the registered user however when clicking the lick it takes me back to a register page, please assist ASAP.

Bellow is my SendActivationEmail methos

please Help.

private void SendActivationEmail(int userId)
    {
        string constr = ConfigurationManager.ConnectionStrings["LoginDBConnectionString"].ConnectionString;
        string activationCode = Guid.NewGuid().ToString();
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivation VALUES(@UserId, @ActivationCode)"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@UserId", userId);
                    cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        using (MailMessage mm = new MailMessage("email@gmail.com", txtEmail.Text))
        {
            mm.Subject = "Account Activation";
            string body = "Hello " + txtUsername.Text.Trim() + ",";
            body += "<br /><br />Please click the following link to activate your account";
            body += "<br /><a href = '"+ Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
            body += "<br /><br />Thanks";
            mm.Body = body;
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential("email@gmail.com", "Password");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);

Dani AI

Generated

Quick diagnosis for (and thanks to for the pointer): the email is sending a link back to the registration page because the code builds the activation URL by doing a string Replace on the current page URL. If the Replace target (the filename you expect) doesn't exist in the current URL, Replace returns the original URL unchanged — so you end up mailing the register page. There’s also a filename mismatch between the activation page you’re generating and the actual .aspx page name; make those consistent.

Instead of Replace, construct the activation URL explicitly from the request context. For example:

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority
                 + (Request.ApplicationPath == "/" ? "" : Request.ApplicationPath.TrimEnd('/'));
string activationLink = baseUrl + "/Activation.aspx?ActivationCode=" + HttpUtility.UrlEncode(activationCode);

Use that activationLink as the href in your HTML email.

Practical checks and tips:

  • Confirm the DB insert succeeded before sending the email (use an explicit column list in INSERT, e.g. INSERT INTO UserActivation(UserId, ActivationCode) VALUES(...)).
  • Verify the email source (view raw message) to ensure the <a href="..."> is not being HTML-escaped.
  • On the SMTP side set smtp.UseDefaultCredentials = false when you assign smtp.Credentials, and for Gmail use an app password or appropriate secure settings.
  • While testing on localhost remember links will point to localhost; test on a reachable host (or use a tunneling tool) if you need external access.

If the link still fails after fixing URL construction, paste the exact generated URL (not the page code) so debugging can focus on the activation page logic.

Recommended Answers

All 3 Replies

If the link is correct in your email then something is probably going wrong on your activation page and it is redirecting to the register page again.
You'd need to debug what happens on the activation page or post it up here.

And this is my Activation Page,

Thanks.

aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Activation.aspx.cs" Inherits="Activation" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1><asp:Literal ID="ltMessage" runat="server" /></h1>
    </div>
    </form>
</body>
</html>




aspx.cs code

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            string constr = ConfigurationManager.ConnectionStrings["LoginDBConnectionString"].ConnectionString;
            string activationCode = !string.IsNullOrEmpty(Request.QueryString["ActivationCode"]) ? Request.QueryString["ActivationCode"] : Guid.Empty.ToString();
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("DELETE FROM UserActivation WHERE ActivationCode = @ActivationCode"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                        cmd.Connection = con;
                        con.Open();
                        int rowsAffected = cmd.ExecuteNonQuery();
                        con.Close();
                        if (rowsAffected == 1)
                        {
                            ltMessage.Text = "Activation successful.";
                        }
                        else
                        {
                            ltMessage.Text = "Invalid Activation code.";
                        }
                    }
                }
            }
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.