Home > ASP.Net > Send Confimation Email after Create User

Send Confimation Email after Create User


Hi

try this example:

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" LoginCreatedUser="false" DisableCreatedUser="true"         
         MailDefinition-BodyFileName="signupmail.txt" OnSendingMail="CreateUserWizard1_OnSendingMail">
        <WizardSteps>
            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
            </asp:CreateUserWizardStep>
            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
            </asp:CompleteWizardStep>
        </WizardSteps>
    </asp:CreateUserWizard>

** signupmail.txt:

Thank you for signing up for a new account at my site. To complete your registration, please follow the link below:

    http://www.mysite.com/authorise.aspx?u=<%NEWUSERID%&gt;

protected void CreateUserWizard1_OnSendingMail(object sender, MailMessageEventArgs e)
    {
        // Create an SQL connection object
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
        {
            // Create a command to get the user ID of the new user
            SqlCommand command = new SqlCommand("SELECT m.UserId FROM aspnet_Membership m INNER JOIN aspnet_Users u ON m.UserId = u.UserId WHERE u.LoweredUserName= LOWER(@UserName) AND m.Email = @Email AND m.IsApproved = 0",conn);

            // Add the parameters
            command.Parameters.AddWithValue("@UserName", MBTCreateUserWizard.UserName.ToString());
            command.Parameters.AddWithValue("@Email", MBTCreateUserWizard.Email.ToString());

            // Execute the query
            conn.Open();
            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                // Read the next row
                reader.Read();

                string userid = reader[0].ToString();

                // Inject the user ID into the mail body
                e.Message.Body = e.Message.Body.Replace("<%NEWUSERID%>", userid);
            }

            // Close everything
            reader.Close();
            conn.Close();
        }
    }

** In authorise.aspx Page

protected void Page_Load(object sender, EventArgs e)
    {
        // Get the user ID from the request string
        string userid = Request["u"];

        if (userid != null && userid != "")
        {
            // Update the account to activate it
            try
            {
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
                {
                    SqlCommand command = new SqlCommand("UPDATE aspnet_Membership SET IsApproved=1 WHERE UserId=@UserId", conn);

                    command.Parameters.AddWithValue("@UserId", userid);

                    conn.Open();
                    command.ExecuteNonQuery();
                    conn.Close();
                }

                // Success, inform or redirect
            }
            catch (SqlException se)
            {
                // Provide appropriate user feedback
            }
        }
        else
        {
            // Provide appropriate user feedback
        }
    }

In web.config:

<membership defaultProvider="MyProvider" userIsOnlineTimeWindow="25">
   <providers>
    <add name="MyProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="Bag_ConnStr" applicationName="/" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
   </providers>
  </membership>

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: