Asp.Net Mail Script in C# and VB.Net

 

Writing your own mail script for a contact page for your own web site may not be that easy, even for experienced web developers, as most of the scripts, available on the internet, can be really confusing. Some of them are written for internal websites where the site administrator contact his subordinates through emails. It servers very little to a website owner who wants to make a webpage through which his customers can contact him.

The following scripts, written in C#, is addressing this issue once and for all.

Contact Form for an ASP.Net Page

Suppose, you own a vehicle hire firm and registered customers can contact you with the particular vehicle they need on a given day.

TheTechnical Details for Mail Script button:

  • Domain Name:autohiring.com
  • Customer Name:Damian Hurst
  • Customer Email:damian-hurst@gmail.com
  • Company Email Address:info@autohiring.com

Name:  
Email:  
Choose the Vehicle:



Query:  
Choose Your Task:

Code in .aspx Page button:

<table width="550px">
<tr>
<td"><asp:Label ID="Label2" runat="server" Text="Name:" Font-Size="Small"></asp:Label>
</td>
<td><asp:TextBox ID="txtName" runat="server" Height="25px" Width="250px"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required" ControlToValidate="txtName">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td><asp:Label ID="Label6" runat="server" Text="Email:" Font-Size="Small"></asp:Label></td>
<td><asp:TextBox ID="txtEmail" runat="server" Height="25px" Width="250px"></asp:TextBox>></td>
<td><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" ErrorMessage="Enter a valid Email address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>Choose One</td>
<td colspan="2">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" BorderWidth="1px" Width="252px">
<asp:ListItem>Car</asp:ListItem>
<asp:ListItem>MPV</asp:ListItem>
<asp:ListItem>Van</asp:ListItem>
<asp:ListItem>Lorry</asp:ListItem>
<asp:ListItem>HGV</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td><asp:Label ID="Label4" runat="server" Text="Query:" Font-Size="Small"></asp:Label></td>
<td><asp:TextBox ID="txtQuery" runat="server" Height="180px" TextMode="MultiLine" Width="250px"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txtQuery" ErrorMessage="Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
</td>
<td><asp:Button ID="btnSend" runat="server" CssClass="centralized" Text="Send the Mail" Height="40px" /></td>
<td><asp:Button ID="btnClear" runat="server" CssClass="centralized" Height="40px" Text="Clear" /></td>
</tr>
<tr>
<td colspan="3"><asp:ImageButton ID="imgThumbsup" runat="server" Visible="False" ImageUrl="" />
</td>
</tr>
<tr><td colspan="3"><asp:Label ID="lblMessage" runat="server" Width="300px"></asp:Label></td>
</tr>
</table>

Validation controls are being used along with the input controls to make sure users fill in the right values.

Next thing, we need to do is to write the code for btnSend button and btnClear button for their respective roles:

When the btnSend button is clicked, the validation controls check the input values on the client side and then send the email.

C# Code for aspx.cs page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// namespaces needed to use classes of mail
using System.Net;
using System.Net.Mail;
public partial class contact : System.Web.UI.Page
{
protected void btnSend_Click1(object sender, EventArgs e)
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress(txtEmail.Text, txtName.Text);
//Add the email of the autohiring.com
message.To.Add("info@autohiring.com");
message.IsBodyHtml = true;
message.Subject = RadioButtonList1.SelectedValue.ToString();
message.Body = txtQuery.Text;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
try
{
//instructions to use SMTP server of autohiring.com
smtp.Host = "127.0.0.1";
smtp.Send(message);
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Thank you. We will reply soon.";
}
catch (SmtpException ex)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = ex.Message;
}
}
protected void btnClear_Click1(object sender, EventArgs e)
{
{
//input controls are cleared
txtEmail.Text = "";
txtName.Text = "";
txtQuery.Text = "";
lblMessage.Text = "";
RadioButtonList1.SelectedIndex =-1;
txtName.Focus();
}
}
}

VB.Net Code for aspx.vb page

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Net
Imports System.Net.Mail
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress(txtEmail.Text, txtName.Text);
'Add the email of the autohiring.com
message.To.Add("info@autohiring.com");
message.IsBodyHtml = true;
message.Subject = RadioButtonList1.SelectedValue.ToString();
message.Body = txtQuery.Text;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
try
'instructions to use SMTP server of autohiring.com
smtp.Host = "127.0.0.1";
smtp.Send(message);
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Thank you. We will reply soon.";
catch (SmtpException ex)
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = ex.Message;

The mail script uses .System.Mail object, its properties and methods to achieve the goal - sending and receiving emails with ease. It is explained above.

The code for btnClear button is the same for VB.Net.

If you want to see a working model of the mail script, please follow the link below:

Click Here

 

 

 

Recommended Reading

Amazon Best Seller

 

Everything is evolving; so is the layout of a text book. By uniquely presenting the rich contents of the book, the author has elevated positive user experience of reading a text book to a new level: the examples are easy to follow and rich in standard. Highly recommended for those who want to master JavaScript and JQuery.

Progressive Web Apps(PWA)


 

The significance of app stores is over; progressive web apps is the next big thing. They are just websites that makes the need of going through app stores and need of storing redundant. They work offline too. If you have a reasonable understanding of HTML, CSS and JavaScript, this is the book for you to learn in no time.

HTML5 Canvas Animations


 

David Geary, in this book, shows how to combine JavaScript and HTML5 Canvas to produce amazing animations; he has set aside a whole chapter to teach you the role of physics in animations. If you want an in-depth understanding about HTML5 Canvas animations, this is a must read.