Sample Registration Page and Email Confirmation in ASP.Net

Introduction

This article explains how to Auto Respond Email sent; once the end-user has registered, a confirmation email will be sent to the registrant to confirm the registration.

Use:

  • ASP .NET web page
  • Create HTML Email Templates
  • JavaScript validation
  • MySQL Database

First, for the previous article read and use the following ”Table Structure for Table (Event)”.

  1. CREATE TABL EIF NOTEXISTS `Event` (  
  2.   `Firstname` Varchar(100)NOTNULL,  
  3.   `Lastname` Varchar(100)NOTNULL,  
  4.   `Email` Varchar(25)NOTNULL,  
  5.   `Mobile` Varchar(12)NOTNULL  
  6. )ENGINE=InnodbDEFAULTCHARSET=Latin1;
Step 1: Create a new project using "File" > "New" > "Project..." > "ASP .NET Empty Web Application".



Step 2: Go to Solution Explorer then right-click on your application then select "Add New Item" then select "Html Page" and name it “Event.html”.



Step 3: Use the following Source code for the “Event.Html” in the Design Page View.



Step 4: Design the HTML page with the following markup: “Event.Html”.



Step 5: Now in the design “Registration.aspx“ design the web page with the following source code. I have also implemented the JavaScript validations on the First Name, Last Name, Email ID and the Mobile fields.

Registration.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs"  
  2. Inherits="RegistrationEmail.Registration" Title="Registration & Email Sending" %>  
  3. <!DOCTYPE html>  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Registration web form with HTML Email Template</title>  
  7.     <link href="StyleSheet.css" rel="stylesheet" />  
  8.     <script type="text/javascript">  
  9.         function validationCheck() {  
  10.             var summary = "";           
  11.             summary += isvalidFirstname();  
  12.             summary += isvalidLastname();  
  13.             summary += isvalidEmail();  
  14.             summary += isvalidMobileno();          
  15.             if (summary != "") {  
  16.                 alert(summary);  
  17.                 return false;  
  18.             }  
  19.             else {  
  20.                 return true;  
  21.             }  
  22.         }  
  23.         function isvalidFirstname() {  
  24.             var id;  
  25.             var temp = document.getElementById("<%=txtFirstName.ClientID %>");  
  26.             id = temp.value;  
  27.             if (id == "") {  
  28.                 return ("Please enter First Name" + "\n");  
  29.             }  
  30.             else {  
  31.                 return "";  
  32.             }  
  33.         }  
  34.         function isvalidLastname() {  
  35.             var id;  
  36.             var temp = document.getElementById("<%=txtLastName.ClientID %>");  
  37.             id = temp.value;  
  38.             if (id == "") {  
  39.                 return ("Please enter Last Name" + "\n");  
  40.             }  
  41.             else {  
  42.                 return "";  
  43.             }  
  44.         }  
  45.         function isvalidEmail() {  
  46.             var id;  
  47.             var temp = document.getElementById("<%=txtEmailID.ClientID %>");  
  48.             id = temp.value;  
  49.             var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;  
  50.             if (id == "") {  
  51.                 return ("Please Enter Email" + "\n");  
  52.             }  
  53.             else if (re.test(id)) {  
  54.                 return "";  
  55.             }  
  56.   
  57.             else {  
  58.                 return ("Email should be in the form [email protected]" + "\n");  
  59.             }  
  60.         }  
  61.         function isvalidMobileno() {  
  62.             var id;  
  63.             var temp = document.getElementById("<%=txtMobile.ClientID %>");  
  64.             id = temp.value;  
  65.             var re;  
  66.             re = /^[0-9]+$/;  
  67.             var digits = /\d(10)/;  
  68.             if (id == "") {  
  69.                 return ("Please Enter Mobile no" + "\n");  
  70.             }  
  71.             else if (re.test(id)) {  
  72.                 return "";  
  73.             }  
  74.   
  75.             else {  
  76.                 return ("Phone no should be digits only" + "\n");  
  77.             }  
  78.         }  
  79.          
  80. </script>  
  81. </head>  
  82. <body>  
  83.     <form id="form1" runat="server">  
  84.         <div class="page">        
  85.         <div class="center">  
  86.             
  87.                 <table style="width: 100%"; >  
  88.                 <tr>  
  89.                     <td colspan="3">  
  90.                         <h3>Registration web form with HTML Email Template</h3>  
  91.                     </td>  
  92.                 </tr>  
  93.                 <tr>  
  94.                     <td>First Name:</td>  
  95.                     <td>  
  96.                         <asp:TextBox ID="txtFirstName" runat="server" CssClass="txt"></asp:TextBox></td>  
  97.                     <td> </td>  
  98.                 </tr>  
  99.                 <tr>  
  100.                     <td>Last Name:</td>  
  101.                     <td>  
  102.                         <asp:TextBox ID="txtLastName" runat="server" CssClass="txt"></asp:TextBox></td>  
  103.                     <td> </td>  
  104.                 </tr>  
  105.                 <tr>  
  106.                     <td>Email ID:</td>  
  107.   
  108.                     <td>  
  109.                         <asp:TextBox ID="txtEmailID" runat="server" CssClass="txt"></asp:TextBox></td>  
  110.                     <td> </td>  
  111.                 </tr>  
  112.                 <tr>  
  113.                     <td>Mobile:</td>  
  114.   
  115.                     <td>  
  116.                         <asp:TextBox ID="txtMobile" runat="server" CssClass="txt"></asp:TextBox></td>  
  117.                     <td> </td>  
  118.                 </tr>  
  119.                 <tr>  
  120.                     <td></td>  
  121.                     <td colspan="2">  
  122.                         <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"  
  123. CssClass="button" /></td>  
  124.                 </tr>  
  125.             </table>  
  126.   
  127.         </div>  
  128.         </div>  
  129.     </form>  
  130. </body>  
  131. </html>


Step 6: Now, in the code behind file “Registration.aspx.cs“ use the following code.

Registration.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. //Using namespaces  
  9. using MySql.Data.MySqlClient;  
  10. using System.Configuration;  
  11. using System.Text;  
  12. using System.Net;  
  13. using System.Net.Mail;  
  14.   
  15. namespace RegistrationEmail  
  16. {  
  17.     public partial class Registration : System.Web.UI.Page  
  18.     {  
  19.         #region MySqlConnection Connection  
  20.         MySqlConnection conn = new  
  21. MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);  
  22.         #endregion  
  23.         protected void Page_Load(object sender, EventArgs e)  
  24.   
  25.         {  
  26.             Try  
  27.             {  
  28.                 if (!Page.IsPostBack)  
  29.                 {  
  30.                     btnSubmit.Attributes.Add("onclick""javascript:return validationCheck()");                     
  31.                 }  
  32.             }  
  33.             catch (Exception ex)  
  34.             {  
  35.                 ShowMessage(ex.Message);  
  36.             }  
  37.         }  
  38.         /// <summary>  
  39.         /// This function is used for show message.  
  40.         /// </summary>  
  41.         /// <param name="msg"></param>  
  42.         void ShowMessage(string msg)  
  43.   
  44.         {  
  45.             ClientScript.RegisterStartupScript(Page.GetType(), "validation""<script language='javascript'>alert('" + msg + "');</script>");  
  46.         }  
  47.         /// <summary>  
  48.         /// This Function is used TextBox Empty.  
  49.         /// </summary>  
  50.         void clear()  
  51.         {  
  52.             txtFirstName.Text = string.Empty; txtLastName.Text = string.Empty; txtEmailID.Text = string.Empty; txtMobile.Text = string.Empty;  
  53.             txtFirstName.Focus();  
  54.         }  
  55.  
  56.         #region This code user to Email Sending Server.MapPath "Event.html" mailbody.Replace using html email templates  
  57.         private void SendMail()  
  58.         {            
  59.             string filename = Server.MapPath("~/Event.html");  
  60.             string mailbody = System.IO.File.ReadAllText(filename);  
  61.             mailbody = mailbody.Replace("##NAME##", txtFirstName.Text);  
  62.             mailbody = mailbody.Replace("##FirstName##", txtFirstName.Text);  
  63.             mailbody = mailbody.Replace("##LastName##", txtLastName.Text);            
  64.   
  65.             mailbody = mailbody.Replace("##Mobile##", txtMobile.Text);  
  66.             string to = txtEmailID.Text;  
  67.             string from = "[email protected]";  
  68.             MailMessage message = new MailMessage(from, to);  
  69.             message.Subject = "Auto Response Email";  
  70.             message.Body = mailbody;  
  71.             message.BodyEncoding = Encoding.UTF8;  
  72.             message.IsBodyHtml = true;  
  73.             SmtpClient client = new SmtpClient("smtp.gmail.com", 587);  
  74.             System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("[email protected]""Password");  
  75.             client.EnableSsl = true;  
  76.             client.UseDefaultCredentials = true;  
  77.             client.Credentials = basicCredential;  
  78.             try  
  79.             {  
  80.                 client.Send(message);  
  81.                 ShowMessage("Email Sending successfully...!");                  
  82.             }  
  83.             catch (Exception ex)  
  84.             {  
  85.                 ShowMessage(ex.Message);  
  86.             }  
  87.         }  
  88.         #endregion  
  89.  
  90.         #region This Code used to Insert data and Send Email  
  91.         protected void btnSubmit_Click(object sender, EventArgs e)  
  92.         {  
  93.             Try  
  94.             {  
  95.                 conn.Open();  
  96.                 MySqlCommand cmd = new MySqlCommand("Insert into event (FirstName,LastName,Email,Mobile) values (@FirstName,@LastName,@Email,@Mobile)", conn);  
  97.                 cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);  
  98.                 cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);  
  99.                 cmd.Parameters.AddWithValue("@Email", txtEmailID.Text);  
  100.                 cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text);  
  101.                 cmd.ExecuteNonQuery();                
  102.                 cmd.Dispose();  
  103.                 ShowMessage("Registered successfully......!");  
  104.                 SendMail();  
  105.                 clear();  
  106.             }  
  107.             catch (MySqlException ex)  
  108.             {  
  109.                 ShowMessage(ex.Message);  
  110.             }  
  111.             Finally  
  112.             {  
  113.                 conn.Close();  
  114.             }  
  115.         }  
  116.         #endregion  
  117.     }  
  118. } 
Step 7: Use the following for Run -> “Registration.aspx“ to design the web page.



Step 8: Now, show in the Message box “Registered Successfully”.



Step 9: Now "Email Account Open" - > "View Email".



I hope this article is useful. If you have any other questions then please provide your comments below.


Similar Articles