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)”.
- CREATE TABL EIF NOTEXISTS `Event` (
- `Firstname` Varchar(100)NOTNULL,
- `Lastname` Varchar(100)NOTNULL,
- `Email` Varchar(25)NOTNULL,
- `Mobile` Varchar(12)NOTNULL
- )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.aspxStep 6: Now, in the code behind file “Registration.aspx.cs“ use the following code.
Registration.aspx.cs
- 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 MySql.Data.MySqlClient;
- using System.Configuration;
- using System.Text;
- using System.Net;
- using System.Net.Mail;
-
- namespace RegistrationEmail
- {
- public partial class Registration : System.Web.UI.Page
- {
- #region MySqlConnection Connection
- MySqlConnection conn = new
- MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
- #endregion
- protected void Page_Load(object sender, EventArgs e)
-
- {
- Try
- {
- if (!Page.IsPostBack)
- {
- btnSubmit.Attributes.Add("onclick", "javascript:return validationCheck()");
- }
- }
- catch (Exception ex)
- {
- ShowMessage(ex.Message);
- }
- }
-
-
-
-
- void ShowMessage(string msg)
-
- {
- ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('" + msg + "');</script>");
- }
-
-
-
- void clear()
- {
- txtFirstName.Text = string.Empty; txtLastName.Text = string.Empty; txtEmailID.Text = string.Empty; txtMobile.Text = string.Empty;
- txtFirstName.Focus();
- }
-
- #region This code user to Email Sending Server.MapPath "Event.html" mailbody.Replace using html email templates
- private void SendMail()
- {
- string filename = Server.MapPath("~/Event.html");
- string mailbody = System.IO.File.ReadAllText(filename);
- mailbody = mailbody.Replace("##NAME##", txtFirstName.Text);
- mailbody = mailbody.Replace("##FirstName##", txtFirstName.Text);
- mailbody = mailbody.Replace("##LastName##", txtLastName.Text);
-
- mailbody = mailbody.Replace("##Mobile##", txtMobile.Text);
- string to = txtEmailID.Text;
- string from = "[email protected]";
- MailMessage message = new MailMessage(from, to);
- message.Subject = "Auto Response Email";
- message.Body = mailbody;
- message.BodyEncoding = Encoding.UTF8;
- message.IsBodyHtml = true;
- SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
- System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("[email protected]", "Password");
- client.EnableSsl = true;
- client.UseDefaultCredentials = true;
- client.Credentials = basicCredential;
- try
- {
- client.Send(message);
- ShowMessage("Email Sending successfully...!");
- }
- catch (Exception ex)
- {
- ShowMessage(ex.Message);
- }
- }
- #endregion
-
- #region This Code used to Insert data and Send Email
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- Try
- {
- conn.Open();
- MySqlCommand cmd = new MySqlCommand("Insert into event (FirstName,LastName,Email,Mobile) values (@FirstName,@LastName,@Email,@Mobile)", conn);
- cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
- cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
- cmd.Parameters.AddWithValue("@Email", txtEmailID.Text);
- cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text);
- cmd.ExecuteNonQuery();
- cmd.Dispose();
- ShowMessage("Registered successfully......!");
- SendMail();
- clear();
- }
- catch (MySqlException ex)
- {
- ShowMessage(ex.Message);
- }
- Finally
- {
- conn.Close();
- }
- }
- #endregion
- }
- }
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.