How to Send user Confirmation email after Registration in ASP.Net csharp coding


How to Send Confirmation Email Link to User E-Mail After Registration Done in ASP.NET using C# Programming

This is Srikanth from Hyderabad, in these tutorials i'm going to explain How to Send Account Activation Link to User E-Mail After Registration Done in ASP.NET using C# Programming.
In previous article I explained How to Send Bulk Emails by Using Gmail Account. Now i show How to Send User Confirmation Email after Registration in ASP.NET by using C# Coding and Resolve a SMTP Server Secure Connection and Authentication 5.5.1 Required Error | The SMTP Server Requires a Secure Connection or the Client was not authenticated.
This Article give brief information and coding to send user Confirmation EMail after registration done. In Order to validate User Email ID at the Registration time, and Account Activation Link Sent to the Email ID and when user clicks the link in his Email ID after User Account Convert to Inactive Mode - to - Active Mode

Create DataBase

USE [master]
GO
/****** Object:  Table [dbo].[Registration]    Script Date: 04/07/2015 11:31:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Registration](
 [username] [nvarchar](50) NULL,
 [password] [nvarchar](50) NULL,
 [emailid] [nvarchar](50) NULL,
 [activation_code] [uniqueidentifier] NULL,
 [account_status] [nvarchar](15) NULL
) ON [PRIMARY]
GO

HTML Markup : Create Registration Page

How to Send user Confirmation email after Registration in ASP.Net csharp programming
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table class="table">
     <tr>
      <th class="table_th" colspan="2">Registration</th>
     </tr>
     <tr>
      <td colspan="2"></td>
     </tr>
      <tr>
       <td style="width:200px; text-align:center;">Username :</td>
       <td><asp:TextBox ID="txtusername" runat="server" CssClass="txtbox"></asp:TextBox>
           <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
               ControlToValidate="txtusername" >enter username</asp:RequiredFieldValidator>
          </td>
      </tr>
      <tr>
       <td style="width:200px; text-align:center;">Password :</td>
       <td><asp:TextBox ID="txtpassword" runat="server" CssClass="txtbox" TextMode="Password"></asp:TextBox>
           <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
               ControlToValidate="txtpassword">enter password</asp:RequiredFieldValidator>
          </td>
      </tr>
      <tr>
      <td style="width:200px; text-align:center;">Email ID :</td>
      <td><asp:TextBox ID="txtemailid" runat="server" CssClass="txtbox"></asp:TextBox><asp:RegularExpressionValidator
              ID="RegularExpressionValidator1" runat="server" 
              ControlToValidate="txtemailid" 
              ValidationExpression="\w+undefined[-+.']\w+)*@\w+undefined[-.]\w+)*\.\w+undefined[-.]\w+)*">please enter valid email addressundefinedabc@xyz.com)</asp:RegularExpressionValidator></td>
      </tr>
      <tr>
      <td colspan="2" align="center">
       <asp:Button ID="Btn_Register" runat="server" Text="Register" 
              onclick="Btn_Register_Click"/>
      </td>
      </tr>
     </table>
    </div>
    </form>
</body>
</html>

C# Coding

C# Coding : Namespace

Namespaceb :
Encode and Decode :
using System.Security.Cryptography;
using System.Text;
Mail :
using System.Net.Mail;
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using System.Net.Mail;

C# Coding : Registration Page

public partial class Registration : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RCCon"].ConnectionString);
    SqlCommand cmd;

    // Send Email to user email address
    public void Send_Account_Activation_Link(string emailaddress, string act_code)
    {
        MailMessage mm = new MailMessage("Your EMail ID", emailaddress);
        mm.Subject = "Account Activation";
        string body = "Hello " + txtusername.Text + ",";
        body += "<br/><br/>Please click the following link to activate your account<br/>";
        body += "<br/><br/><a style='background:#000000; color:#fafafa; padding:10px 100px 10px 100px; width:350px; text-decoration:none; font-weight:bold; font-size:20px;' href = '"+ Request.Url.AbsoluteUri.Replaceundefined"Registration.aspx", "Account_Activation.aspx?ActivationCode=" + act_code) + "'>Click here to activate your account.</a>";
        body += "<br /><br />Thanks";
        mm.Body = body;
        mm.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.Credentials = new System.Net.NetworkCredential("Your Email ID", "");
        smtp.EnableSsl = true;
        smtp.Send(mm);
    }

C# Coding : Button Click Event

    protected void Btn_Register_Click(object sender, EventArgs e)
    {
        string activationCode = Guid.NewGuid().ToString();
        string encry_password = Encrypt_Password(txtpassword.Text);
        cmd = new SqlCommand("insert into Registration values('" + txtusername.Text.ToLower() + "','" + encry_password + "','" + txtemailid.Text + "','" + activationCode + "','inactive')");
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        Send_Account_Activation_Link(txtemailid.Text, activationCode);
        con.Close();
        Session["user"] = txtusername.Text;
        Response.Redirect("Account_Activation.aspx");
    }

C# Coding : Encript Method

    private string Encrypt_Password(string password)
    {
        string pwdstring = string.Empty;
        byte[] pwd_encode = new byte[password.Length];
        pwd_encode = Encoding.UTF8.GetBytes(password);
        pwdstring = Convert.ToBase64String(pwd_encode);
        return pwdstring;
    }   
}

HTML Markup : Create Login Page

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table class="table">
     <tr>
      <th colspan="2" class="table_th">Login Page</th>
     </tr>
      <tr >
       <td>Username :</td>
       <td><asp:TextBox ID="txtusername" runat='server' CssClass="txtbox"></asp:TextBox></td>
      </tr>
      <tr>
       <td >Password :</td>
       <td><asp:TextBox id="txtpassword" runat="server" CssClass="txtbox" TextMode="Password"></asp:TextBox></td>
      </tr>
      <tr>
       <td colspan="2" align="center">
           <asp:Button ID="Btn_Login" runat="server" onclick="Btn_Login_Click" Text="Login" />
       </td>
      </tr>
      <tr>
       <td colspan="2">
         Create a New Account : <a href="Registration.aspx">SignUp</a>
       </td>
      </tr>
      <tr>
       <td colspan="2" align="left">
        <label id="lblerror" runat="server"></label>
       </td>
      </tr>
     </table>
    </div>
    </form>
</body>
</html>

C# Coding : Code for Login Page

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.IO;

public partial class login : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RCCon"].ConnectionString);
     
    protected void Btn_Login_Click(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from Registration", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string userid = ds.Tables[0].Rows[i]["username"].ToString();
                string pwd = Decrypt_Password(ds.Tables[0].Rows[i]["password"].ToString());
                string status = "active";
                if (status == ds.Tables[0].Rows[i]["account_status"].ToString())
                {
                    
                    if (userid == txtusername.Text.ToLower() && pwd == txtpassword.Text)
                    {
                        Response.Redirect("Default.aspx?Username=" + txtusername.Text);
                    }
                }
                
                    lblerror.InnerText = "Invalid Username and Password";

            }
        }
    }    

C# Coding : Decrypt Password Method

    private string Decrypt_Password(string encryptpassword)
    {
        string pwdstring = string.Empty;
        UTF8Encoding encode_pwd = new UTF8Encoding();
        Decoder Decode = encode_pwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpassword);
        int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        pwdstring = new String(decoded_char);
        return pwdstring;
    }
}

Acknowledgement for Activation Link Sent



Activation Link in Email Address



After Click the Activation Link in Email | Account Activated Message Appears




Next Tutorials : How to Resove SMTPException




The SMTP SEVER Requires Secure Connection or the Client was not Authenticated the Server Response was 5.5.1 Authentication Required
9 Komentar untuk "How to Send user Confirmation email after Registration in ASP.Net csharp coding"

Does this code will also run at on that website which is hosted on webserver

please help me Im getting error as nullreferenceexception was unhandled by user code

what should I do????

you have to give your gmail Id and password otherwise its give error

did you done any modification in code....

no..its working now...but how can I add a link for my login page in email body?

hey everything is working fine but only one problem is there........after filling the login username and password it is showing invalid username and password also after filling the correct username and password...please help me...otherwise everything is working

you have to do some setting in your gmail account, for that just check this article how to do gmail setting to send emails

http://allittechnologies.blogspot.in/2015/04/the-smtp-server-requires-secure-connection-or-the-client-was-not-authenticated-the-server-response-was-5.5.1-authentication-required.html

Back To Top