How to implement user registration with email confirmation using ASP.NET

Want to learn how to write code for sending activation links to new registered members on a website. This article is about using ASP.NET membership feature and sending Email to newly registered Member a email to confirm link.

Lets begin with the Registration Feature of ASP.NET membership that will send Email with confirmation code or Activation link to the newly registered email id.

When user will click link, then account will be active according to the code or link we have send to the email id.

Lets begin with the assets we have:
Visual Studio 2012 Ultimate
Windows 8 Pro
MS Sql Server 2008 R2
C# for Backend Language

I am here assuming that you have the basic knowledge of ASP.NET.

Step 1: Creating a Website

Open Visual Studio and Click on New >> Website and Choose Empty Website in your Required Framework version in Required Language (vb or c#).

Step 2: Add New Page and Control

Right Click on Solution Name and Click Add Item and Choose Web Form.
We need at least below Pages and Items in Solution.

Home Page (Default.aspx)
Login Page (Login.aspx)
Registration (Register.aspx)
Confirm Page (Confirm.aspx)
Link for Mail (ConfirmationCode.html)
Additional forms and Folders as per requirement.

What we will do in above forms.

In Default.aspx we will put a Login View (Login/Logout) LoginName control.
In Login.aspx we are putting a Login Control in this Page.
In Register.aspx we will put a CreateUserWizard control to register.
In Confirm.aspx we will put some code in Backend and accept the Confirmation Code based in URL, or Entered Code.
In ConfirmationCode.html we will put Generated Code for Sending Mail. This will be attachment of Mail Body.

Open your Toolbox of Visual Studio and Insert a CreateUserWizard from Login Section in Form according to your Design as mentioned above and other designs.

Step 3:Customization and Accessories

We need to add some note in web.config file. Here is the Code that is required in web.config.

<add name="ApplicationServices" connectionString="data source=.\sqldb;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
<compilation debug="true" targetFramework="4.0"/>
<authentication mode="Forms">
<forms loginUrl ="Login.aspx" timeout="200"></forms>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"
connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true"
requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
<location path="Default.aspx">
<deny users ="?"/>
<location path="Register.aspx">
<allow users="*"/>

Replace your Instance ID in above ID for Connection String.

Lets Type the Body of HTML Message that will be send on Registration.

<html xmlns="">
<title>Confirm Your Registration</title>
Hi <b><%Username%></b>,<br />

We are feeling Glad to welcome you in our Community. <br />

You are just one step far from your friends. Please click below link to confirm your Registration and get in touch.<br />
<br />
<br /><br />
Webmaster,<br />
P.Yar.B Complex

We have put two Variables Usernames and VerificationUrl in Message and will replace these keywords before sending mail.

Open the Properties of Registration Control and by Pressing F4 after selecting Control or Right Click and Choosing Property.

Set DisableCreateUser option to True

Also set MailDefinition for sending mails and Define attachment file.

<MailDefinition From="" Subject="Confirmation Link" IsBodyHtml="true" BodyFileName="~/ConfirmationCode.html">

Now Right Click on CreateUserWizard Control and Choose Event after selecting Property window.
From the Events write this code on SendingMail Event.
Lets see the Code for Sending Mail.
Namespaces Required:
System.Net for Sending Mail and System.Web.Security to access Membership features.

protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
MembershipUser newUserAccount = Membership.GetUser(CreateUserWizard1.UserName);
Guid newUserAccountId = (Guid)newUserAccount.ProviderUserKey;
string domainName = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
string confirmationPage = "/Confirm.aspx?memberID=" + newUserAccountId.ToString();
string url = domainName + confirmationPage;
e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", url);
SmtpClient smtp = new SmtpClient();
smtp.Host = "";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("", "xxxxxxxxxx");
smtp.EnableSsl = true;
e.Cancel = true;

Description of Above Code:
Line 1: We get the Username of Currently Created User.
Line 2: We get UserKey of currently Created User and converted it to GUID Format.
Line 3: We find name of Domain where it is being Hosted. Only Domain (Left Part before / and Application path if any).
Line 4: We put path to Confirm page on a String variable to join with Domain.
Line 5: We joined Confirmation Link and Domain Name to form a Complete URL.
Line 6: We Replaced Keywords of Mail Body, such as VerificationUrl with exact values.
Line 7 onward: We are sending Mail using Google SMTP Servers, so rest is the Same Process.
Replace Email ID and Password with your Google ID/Password to send Mail.

Put a Additional Textbox, if you want to Put Code also. But in above case, we are just using Confirmation Link, so place a Link in this Page to set the Status of Success.

Now What we will do is, First Find GUID from QueryString (passed in the ConfirmationLink.html page and send to Email ID) and activate the user.

Code for backend in Confirm.aspx

Guid newUserId = new Guid(Request.QueryString["memberID"]);
MembershipUser newUser = Membership.GetUser(newUserId);
if (newUser == null)
Label1.Text = "User Account not found";
newUser.IsApproved = true;
Label1.Text = "Account Approved, please click <a href='~/Login.aspx' target='_self'>login</a> to continue";

Now your Account is active and you can login to your account.

You can download the Source Code. Leave comments if you have any feedback/suggestions.

All the Best.


  • Source Code (
  • Comments

    Guest Author: Hariharan venkitasub29 Mar 2013


    The sources are good one and really great job.
    I have already created a sign up page (not with the create user wizard) but
    with asp coding, i need to include this email verification for new users.
    How i can do it, kindly guide me with your opinion


    Author: John Bhatt06 Apr 2013 Member Level: Gold   Points : 4

    Hello Hariharan,
    Thanks for reading article and providing feedback.

    While coming on your question, you can send mail at the Button Click event. Suppose,
    you have created a SqlDataAdaptor, dataset/datatable and fill this on Register or Submit button click.
    Check if record is inserted, your table has a field of Bit type and initially set to false.
    Then generate a unique Id for each user on registration and put that value on Database also.
    Then simply pass that value to email and normal as above.

    Hope this will give you rough idea.

    All the Best.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: