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:

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. else {
  57. return ("Email should be in the form [email protected]" + "\n");
  58. }
  59. }
  60. function isvalidMobileno() {
  61. var id;
  62. var temp = document.getElementById("<%=txtMobile.ClientID %>");
  63. id = temp.value;
  64. var re;
  65. re = /^[0-9]+$/;
  66. var digits = /\d(10)/;
  67. if (id == "") {
  68. return ("Please Enter Mobile no" + "\n");
  69. }
  70. else if (re.test(id)) {
  71. return "";
  72. }
  73. else {
  74. return ("Phone no should be digits only" + "\n");
  75. }
  76. }
  77. </script>
  78. </head>
  79. <body>
  80. <form id="form1" runat="server">
  81. <div class="page">
  82. <div class="center">
  83. <table style="width: 100%"; >
  84. <tr>
  85. <td colspan="3">
  86. <h3>Registration web form with HTML Email Template</h3>
  87. </td>
  88. </tr>
  89. <tr>
  90. <td>First Name:</td>
  91. <td>
  92. <asp:TextBox ID="txtFirstName" runat="server" CssClass="txt"></asp:TextBox></td>
  93. <td> </td>
  94. </tr>
  95. <tr>
  96. <td>Last Name:</td>
  97. <td>
  98. <asp:TextBox ID="txtLastName" runat="server" CssClass="txt"></asp:TextBox></td>
  99. <td> </td>
  100. </tr>
  101. <tr>
  102. <td>Email ID:</td>
  103. <td>
  104. <asp:TextBox ID="txtEmailID" runat="server" CssClass="txt"></asp:TextBox></td>
  105. <td> </td>
  106. </tr>
  107. <tr>
  108. <td>Mobile:</td>
  109. <td>
  110. <asp:TextBox ID="txtMobile" runat="server" CssClass="txt"></asp:TextBox></td>
  111. <td> </td>
  112. </tr>
  113. <tr>
  114. <td></td>
  115. <td colspan="2">
  116. <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
  117. CssClass="button" /></td>
  118. </tr>
  119. </table>
  120. </div>
  121. </div>
  122. </form>
  123. </body>
  124. </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. namespace RegistrationEmail
  15. {
  16. public partial class Registration : System.Web.UI.Page
  17. {
  18. #region MySqlConnection Connection
  19. MySqlConnection conn = new
  20. MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  21. #endregion
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24. Try
  25. {
  26. if (!Page.IsPostBack)
  27. {
  28. btnSubmit.Attributes.Add("onclick", "javascript:return validationCheck()");
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33. ShowMessage(ex.Message);
  34. }
  35. }
  36. /// <summary>
  37. /// This function is used for show message.
  38. /// </summary>
  39. /// <param name="msg"></param>
  40. void ShowMessage(string msg)
  41. {
  42. ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('" + msg + "');</script>");
  43. }
  44. /// <summary>
  45. /// This Function is used TextBox Empty.
  46. /// </summary>
  47. void clear()
  48. {
  49. txtFirstName.Text = string.Empty; txtLastName.Text = string.Empty; txtEmailID.Text = string.Empty; txtMobile.Text = string.Empty;
  50. txtFirstName.Focus();
  51. }
  52. #region This code user to Email Sending Server.MapPath "Event.html" mailbody.Replace using html email templates
  53. private void SendMail()
  54. {
  55. string filename = Server.MapPath("~/Event.html");
  56. string mailbody = System.IO.File.ReadAllText(filename);
  57. mailbody = mailbody.Replace("##NAME##", txtFirstName.Text);
  58. mailbody = mailbody.Replace("##FirstName##", txtFirstName.Text);
  59. mailbody = mailbody.Replace("##LastName##", txtLastName.Text);
  60. mailbody = mailbody.Replace("##Mobile##", txtMobile.Text);
  61. string to = txtEmailID.Text;
  62. string from = "[email protected]";
  63. MailMessage message = new MailMessage(from, to);
  64. message.Subject = "Auto Response Email";
  65. message.Body = mailbody;
  66. message.BodyEncoding = Encoding.UTF8;
  67. message.IsBodyHtml = true;
  68. SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
  69. System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("[email protected]", "Password");
  70. client.EnableSsl = true;
  71. client.UseDefaultCredentials = true;
  72. client.Credentials = basicCredential;
  73. try
  74. {
  75. client.Send(message);
  76. ShowMessage("Email Sending successfully...!");
  77. }
  78. catch (Exception ex)
  79. {
  80. ShowMessage(ex.Message);
  81. }
  82. }
  83. #endregion
  84. #region This Code used to Insert data and Send Email
  85. protected void btnSubmit_Click(object sender, EventArgs e)
  86. {
  87. Try
  88. {
  89. conn.Open();
  90. MySqlCommand cmd = new MySqlCommand("Insert into event (FirstName,LastName,Email,Mobile) values (@FirstName,@LastName,@Email,@Mobile)", conn);
  91. cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
  92. cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
  93. cmd.Parameters.AddWithValue("@Email", txtEmailID.Text);
  94. cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text);
  95. cmd.ExecuteNonQuery();
  96. cmd.Dispose();
  97. ShowMessage("Registered successfully......!");
  98. SendMail();
  99. clear();
  100. }
  101. catch (MySqlException ex)
  102. {
  103. ShowMessage(ex.Message);
  104. }
  105. Finally
  106. {
  107. conn.Close();
  108. }
  109. }
  110. #endregion
  111. }
  112. }
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.