Now you have to create a simple web form in Visual Studio as shown in the figure. The code of the following web form is given below.
<%@ 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: 100%;
}
.style2
{
width: 156px;
}
.style3
{
width: 156px;
text-align: right;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style3">
Gmail Address</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="255px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Valid Email Address..."
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style3">
Gmail password</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Width="255px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
To</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Width="255px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Subject</td>
<td>
<asp:TextBox ID="TextBox4" runat="server" Width="400px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Message</td>
<td>
<asp:TextBox ID="TextBox5" runat="server" Height="74px" TextMode="MultiLine"
Width="400px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Send Mail" />
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Now your web form is complete and the only task which is remaining is applying the Mail Sending function on the event of Button Click. Before applying the code in the .CS file you have to include following header files on it.
using System.Net;
using System.Net.Mail;
using System.Net.Mail;
Now the code for sending the mail is given as follows:
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(TextBox1.Text, "Senders Name");
msg.To.Add(new MailAddress(TextBox3.Text));
msg.Subject = TextBox4.Text;
msg.Body = TextBox5.Text;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new NetworkCredential(TextBox1.Text, TextBox2.Text);
smtp.EnableSsl = true;
smtp.Send(msg);
}
}
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(TextBox1.Text, "Senders Name");
msg.To.Add(new MailAddress(TextBox3.Text));
msg.Subject = TextBox4.Text;
msg.Body = TextBox5.Text;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new NetworkCredential(TextBox1.Text, TextBox2.Text);
smtp.EnableSsl = true;
smtp.Send(msg);
}
}
Now just compile and run the code.