Follow these steps-
1. Create a Web application or add a new aspx page to your web application.
2. Place controls as required. for example, screen below-
3. Design of ASPX page is:
<%@ 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>Web Mail</title>
<style>
.width100
{
width=100px;
}
.width200
{
width=200px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table style="background-color: Window; border: solid 1px blue; width: 500px; border-color: Blue;">
<tr>
<td colspan="2" style="text-align: center; background-color: Blue; font: bold, 15px;
color: White;">
<asp:Label ID="lblTitle" runat="server" Text="Send Your Feedback"></asp:Label>
</td>
</tr>
<tr>
<td class="width100">
<asp:Label ID="lblName" runat="server" Text="Your Name:"></asp:Label>
</td>
<td class="width200">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="width100">
<asp:Label ID="lblEmail" runat="server" Text="Your E-mail:"></asp:Label>
</td>
<td class="width200">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" style="width: 300px">
<asp:TextBox ID="txtMailBody" runat="server" TextMode="MultiLine" Rows="10" Width="478px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: center;">
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />
</td>
<td style="text-align: center;">
<asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="btnReset_Click" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblStatus" runat="server" Width="81px"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
4. Now change the Code behind of .aspx.cs as below:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
string strUserName = "";
string strUserEmail = "";
string strSubject = "";
string strUserMailBody = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnReset_Click(object sender, EventArgs e)
{
clearFields();
}
private void clearFields()
{
txtEmail.Text = string.Empty;
txtName.Text = string.Empty;
txtMailBody.Text = string.Empty;
lblStatus.Text = string.Empty;
}
protected void btnSend_Click(object sender, EventArgs e)
{
strUserName = txtName.Text;
strUserEmail = txtEmail.Text.ToString().Trim();
strSubject = "User Feedback";
strUserMailBody = txtMailBody.Text;
sendMail(strUserName, strUserEmail, strSubject, strUserMailBody);
}
private void sendMail(string uname, string umail, string sub, string mailbody)
{
string SMTPServerName = "SLONP143EEX1";
SmtpClient client = new SmtpClient(SMTPServerName);
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(umail, uname);
message.From = fromAddress;
message.Subject = sub;
message.Body = mailbody;
message.To.Add(deepak.solanki@xyx.com); /*Site admin email*/
message.CC.Add("sanjeet.kumar@xyz.com");
client.Send(message);
lblStatus.Text = "Mail sent successfully.";
}
catch(Exception ex)
{
lblStatus.Text = "Error : " + ex.ToString();
}
}
}
5. Build and Run.
6. Test by sending mail.
Hope this will help you.