在asp.net注册后确认电子邮件

时间:2016-04-13 15:14:34

标签: asp.net email registration activation email-confirmation

某种程度上我的代码没有完全正常运行。

这些代码很好:

 protected void Page_Load(object sender, EventArgs e)
    {


    }
    SqlConnection con = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
    SqlCommand cmd = new SqlCommand();

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if(Page.IsValid)
        {
            int userId = 0;
            cmd.Connection = con; //assigning connection to command
            cmd.CommandType = CommandType.Text; //representing type of command
            cmd.CommandText = "INSERT INTO UserData values(@Username,@Firstname,@Lastname,@Email,@Password,@CustomerType,@DeliveryAddress,@Zip,@ContactNumber)";

            //adding parameters with value
            cmd.Parameters.AddWithValue("@Username", txtUser.Text);
            cmd.Parameters.AddWithValue("@Firstname", txtFN.Text);
            cmd.Parameters.AddWithValue("@Lastname", txtLN.Text);
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
            cmd.Parameters.AddWithValue("@Password", (txtPW.Text));
            cmd.Parameters.AddWithValue("@CustomerType", RadioButtonList1.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@DeliveryAddress", txtAddress.Text);
            cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
            cmd.Parameters.AddWithValue("@ContactNumber", txtContact.Text);
            con.Open(); //opening connection
            cmd.ExecuteNonQuery();  //executing query
            SendActivationEmail(userId);
            con.Close(); //closing connection
            label_register_success.Text = "Registered Successfully..";


        }
        else
        {
            label_register_success.Text = "Please check the registration errors";
        }
    }

它注册到sql数据库但似乎没有读取

SendActivation(userId)

直到成功注册消息。我把这行代码定位错了吗?顺便说一句,这是我的sendactivation代码:

private void SendActivationEmail(int userId)
    {
        string constr = ("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True")
        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("sender@gmail.com", txtEmail.Text))
        {
            mm.Subject = "Account Activation";
            string body = "Hello " + txtUser.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("sender@gmail.com", "<password>");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }
}

}

请帮帮我们。提前谢谢你。

0 个答案:

没有答案
相关问题