Thursday, September 3, 2009

Send Activation Email after Create User Account


Try these three ways to send email to user to active his account to be able to login to website

————–First Way ————-
    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        //Send email to user for verifying account
        CreateUserWizard cuw = (CreateUserWizard)sender;
        MembershipUser user = Membership.GetUser(cuw.UserName);
        Guid userId = (Guid)user.ProviderUserKey;
        System.Net.Mail.MailMessage EmailMsg = new System.Net.Mail.MailMessage("info@ask.com", CreateUserWizard1.Email);
        EmailMsg.Subject = "Email Verification from ask.com";
        EmailMsg.IsBodyHtml = true;
        EmailMsg.Body = "Thanks for registering with www.ask.com!<br /><br />Your activation link : <a href=http://www.ask.com/activate.aspx?ID=" + userId.ToString() + ">Link</a>.";
        System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
        //This object stores the authentication values
        System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("smtpserverinfo", "smtpserverpassword");

        mailClient.Host = "mail.ask.com";
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = basicAuthenticationInfo;
        mailClient.Send(EmailMsg);    

    }

and in activate.aspx:

    protected void Page_Load(object sender, EventArgs e)
    {
        Guid oGuid = new Guid(Request.QueryString["ID"]);
        //Guid oGuid = new Guid();
        MembershipUser oUser = Membership.GetUser(oGuid);
        if (oUser != null && oUser.IsApproved == false)
        {
            oUser.IsApproved = true;
            Membership.UpdateUser(oUser);
            System.Web.Security.FormsAuthentication.RedirectFromLoginPage(oUser.UserName, false);
        }
    }

———Second Way———–

My code looks like:

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        //Send email to user for verifying account
        CreateUserWizard cuw = (CreateUserWizard)sender;
        MembershipUser user = Membership.GetUser(cuw.UserName);
        Guid userId = (Guid)user.ProviderUserKey;
        System.Net.Mail.MailMessage EmailMsg = new System.Net.Mail.MailMessage("info@ask.com", CreateUserWizard1.Email);
        EmailMsg.Subject = "Email Verification from ask.com";
        EmailMsg.IsBodyHtml = true;
        EmailMsg.Body = "Thanks for registering with www.ask.com!<br /><br />Your activation link : <a href=http://www.ask.com/activate.aspx?ID=" + userId.ToString() + ">Link</a>.";
        System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
        //This object stores the authentication values
        System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("info@ask.com", "password"); //what is this "password" for??

        mailClient.Host = "mail.ask.com";
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = basicAuthenticationInfo;
        mailClient.Send(EmailMsg);      

    }

and in activate.aspx:

     protected void Page_Load(object sender, EventArgs e)
    {
        Guid oGuid = new Guid();
        MembershipUser oUser = Membership.GetUser(oGuid);
        if (oUser != null && oUser.IsApproved == false)
        {
            oUser.IsApproved = true;
            Membership.UpdateUser(oUser);
            System.Web.Security.FormsAuthentication.RedirectFromLoginPage(oUser.UserName, false);
        }
    }

——–Third Way——–

**
<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%>
**
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
        }
    }

Hope it helps…

No comments:

Post a Comment

Post a Comment