We took two TextBoxes, one for the To email address and the other for making comments or any information you want to send. We used one label too that shows that the email is sent to the desired location.
Initial chamber
Step 1. Open Your Visual Studio 2010 and create an Empty Website, provide a suitable name (Email_demo).
Step 2. In Solution Explorer you get your empty website, add a web form by going as in the following.
For Web Form
Email_demo (your empty website) right-click then select Add New Item -> Web Form. Name it Email_demo.aspx.
Design chamber
Next, we will create a design for our application where we drag some control from the toolbox. So open your Email.aspx page and write code like this.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1 {
width: 106px;
}
.style2 {
text-decoration: underline;
font-size: large;
width: 257px;
}
.style3 {
width: 257px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td class="style1"></td>
<td class="style2"><strong>Email System using ASP.NET C#</strong></td>
<td></td>
</tr>
<tr>
<td class="style1"></td>
<td class="style3"></td>
<td></td>
</tr>
<tr>
<td class="style1">Email:</td>
<td class="style3">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="TextBox1" runat="server"
ErrorMessage="Please Provide Email ID" Font-Bold="True" ForeColor="Red">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style1">Comment:</td>
<td class="style3">
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine"></asp:TextBox>
</td>
<td></td>
</tr>
<tr>
<td class="style1"></td>
<td class="style3"></td>
<td></td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="lbmsg" runat="server"></asp:Label>
</td>
<td class="style3">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send Mail" />
</td>
<td></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Your design will look as in the following.
These are the two TextBoxes wherein the first TextBox you need to enter the Email ID of the user to whom you want to send the mail and in the other TextBox you can enter your information, and then click on the button. That's it, you have sent the mail.
Code chamber
Finally open your Email_demo.aspx.cs page where we write our server-side code so that the mail is sent to the desired user. But before that, look at the namespaces that we will include.
You need to include these two namespaces in your application and anyhow if you can't find these namespaces in the intelligence then add them from the References.
Here is your code for Email_demo – Email_demo.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.Net;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add(TextBox1.Text);
msg.Subject = TextBox2.Text;
SmtpClient smt = new SmtpClient();
smt.Host = "smtp.gmail.com";
System.Net.NetworkCredential ntwd = new NetworkCredential();
ntwd.UserName = "[email protected]"; // Your Email ID
ntwd.Password = ""; // Your Password
smt.UseDefaultCredentials = true;
smt.Credentials = ntwd;
smt.Port = 587;
smt.EnableSsl = true;
smt.Send(msg);
lbmsg.Text = "Email Sent Successfully";
lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
}
}
Output chamber
There is a Required Field Validator in the first TextBox, so if you leave the first TextBox then an error will be shown as in the following image.
We will check for the mail. Look at the following image.
I haven't included any body part here, this is the only subject part so if you want to add any body part then for that just use one text box and make the text mode multiline. On the server side you need to provide the following two lines of code.
I hope you like it. Thank you for reading. Have a good day.