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

 

 

Resources at Fingertips

There is a significant selection of tutorials here, covering ASP.Net, HTML5, CSS3 and JavaScript. They are categorized clearly for you to access them easily on any device - desktops/laptops, smartphones and laptops.
There are quite a few of them on HTML5 Canvas and CSS3. In addition, there are tutorials on JavaScript and ASP.Net too.

Email: 

Stand Out - from the crowd

students

"There's no such thing as a free lunch."

The best things in nature are free with no strings attached - fresh air, breathtakingly warm sunshine, scene of meadow on the horizon...

Vivax Solutions, while mimicking nature, offers a huge set of tutorials along with interactive tools for free.

Please use them and excel in the sphere of science education.

Everything is free; not even registration is required.

 

 

Recommended Reading

 

The best book to master HTML5 canvas animations. The author tells you how to manipulate the canvas element with JavaScript in a progressive way - starting from the easier to harder worked examples. It is ideal for anyone who wants to be a game developer in HTML5.

Advertisements

Advertisement