ASP.Net C# Email Feedback for the Current Active Directory User

Default.aspx page:


<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
<asp:Label ID="lblSiteText" runat="server"></asp:Label></h2>
<p>
<asp:TextBox ID="txtEmailBody" runat="server" TextMode="MultiLine" Height="300px"
Width="450px"></asp:TextBox>
</p>
<p>
<asp:CheckBox ID="chkAnonymous" runat="server" autopostback="true"
text="&nbsp;&nbsp;Send Anonymously?"
oncheckedchanged="chkAnonymous_CheckedChanged" />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:CheckBox ID="chkReceiveCopy" runat="server"
text="&nbsp;&nbsp;Send me a copy of this email." />
</p>

<p>
<asp:Button ID="btnSubmit" runat="server" Text="Send"
onclick="btnSubmit_Click" />&nbsp;&nbsp;&nbsp;
<asp:Label ID="lblError" runat="server"></asp:Label>
</p>

</asp:Content>

Default.aspx.cs page:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Web.Configuration;
using System.DirectoryServices;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblSiteText.Text = WebConfigurationManager.AppSettings["SiteText"];
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
lblError.Text = SendMail(WebConfigurationManager.AppSettings["EmailTo"]
, WebConfigurationManager.AppSettings["EmailFrom"]
, WebConfigurationManager.AppSettings["EmailCC"]
, WebConfigurationManager.AppSettings["EmailSubject"]
, txtEmailBody.Text);
}
public string SendMail(string toList, string from, string ccList, string subject, string body)
{

MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;

if (!chkAnonymous.Checked)
{
string principal = this.Context.User.Identity.Name.Split(new char[] { '\\' })[1];
string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", principal);
string[] properties = new string[] { "fullname" };
try
{
DirectoryEntry adRoot = new DirectoryEntry(WebConfigurationManager.AppSettings["DomainFull"], WebConfigurationManager.AppSettings["DomainLogin"], WebConfigurationManager.AppSettings["DomainPassword"], AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(adRoot);
searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter = filter;
SearchResult result = searcher.FindOne();
DirectoryEntry directoryEntry = result.GetDirectoryEntry();
string email = directoryEntry.Properties["mail"][0].ToString();
from = email;
}
catch (Exception ee)
{
Response.Write(ee.Message);
}
}

if (chkReceiveCopy.Checked)
{
ccList = from;
}

try { MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList); message.Subject = subject; message.IsBodyHtml = true;
message.Body = body; smtpClient.Host = WebConfigurationManager.AppSettings["SMTPHost"];
smtpClient.Port = Convert.ToInt32(WebConfigurationManager.AppSettings["SMTPHostPort"]);
smtpClient.UseDefaultCredentials = true;
smtpClient.Send(message); msg = "Your email was sent successfully.";
}
catch (Exception ex)
{
msg = ex.Message + " The error has been logged and will get resolved soon. Please try again later.";
}
return msg;
}

protected void chkAnonymous_CheckedChanged(object sender, EventArgs e)
{
if (chkAnonymous.Checked)
{
chkAnonymous.Text = @"&nbsp;&nbsp;&nbsp;This will be sent anonymously.";
chkReceiveCopy.Checked = false;
chkReceiveCopy.Visible = false;
}
else
{
chkAnonymous.Text = @"&nbsp;&nbsp;&nbsp;Send Anonymously?";
chkReceiveCopy.Visible = true;
}
}
}

Site.Master


<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div>
<div>
<div>
<h1><asp:Label ID="lblSiteTitle" runat="server"></asp:Label>
</h1>
</div>
<div>
</div>
<div>
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="Default.aspx" Text="Home"/>
<asp:MenuItem NavigateUrl="About.aspx" Text="About"/>
</Items>
</asp:Menu>
</div>
</div>
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div>
</div>
</div>
<div>

</div>
</form>
</body>
</html>

Site.Master.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;

public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
lblSiteTitle.Text = WebConfigurationManager.AppSettings["SiteTitle"];
}
}

Web.Config


<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="SiteTitle" value="Send Feedback"/>
<add key="SiteText" value="Email your feedback."/>

<add key="DomainFull" value="LDAP://Server/OU=My,OU=LDAPPath,DC=MyDomain,DC=Com" />
<add key="DomainLogin" value="activedirectoryserviceaccountlogin"/>
<add key="DomainPassword" value="activedirectoryserviceaccountpassword"/>
<add key="EmailTo" value="myaddress@mydomain.com"/>
<add key="EmailFrom" value="anonymousaddress@mydomain.com"/>
<add key="EmailCC" value=""/>
<add key="EmailSubject" value="Email Subject"/>
<add key="SMTPHost" value="SMTPHost.MyDomain.com"/>
<add key="SMTPHostPort" value="25"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
<authentication mode="Windows"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

Site.css


/* DEFAULTS
----------------------------------------------------------*/

body
{
background: #b6b7bc;
font-size: .80em;
font-family: "Helvetica Neue", "Lucida Grande", "Segoe UI", Arial, Helvetica, Verdana, sans-serif;
margin: 0px;
padding: 0px;
color: #696969;
}

a:link, a:visited
{
color: #034af3;
}

a:hover
{
color: #1d60ff;
text-decoration: none;
}

a:active
{
color: #034af3;
}

p
{
margin-bottom: 10px;
line-height: 1.6em;
}

/* HEADINGS
----------------------------------------------------------*/

h1, h2, h3, h4, h5, h6
{
font-size: 1.5em;
color: #666666;
font-variant: small-caps;
text-transform: none;
font-weight: 200;
margin-bottom: 0px;
}

h1
{
font-size: 1.6em;
padding-bottom: 0px;
margin-bottom: 0px;
}

h2
{
font-size: 1.5em;
font-weight: 600;
}

h3
{
font-size: 1.2em;
}

h4
{
font-size: 1.1em;
}

h5, h6
{
font-size: 1em;
}

/* this rule styles <h1> and <h2> tags that are the
first child of the left and right table columns */
.rightColumn > h1, .rightColumn > h2, .leftColumn > h1, .leftColumn > h2
{
margin-top: 0px;
}

/* PRIMARY LAYOUT ELEMENTS
----------------------------------------------------------*/

.page
{
width: 960px;
background-color: #fff;
margin: 20px auto 0px auto;
border: 1px solid #496077;
}

.header
{
position: relative;
margin: 0px;
padding: 0px;
background: #4b6c9e;
width: 100%;
}

.header h1
{
font-weight: 700;
margin: 0px;
padding: 0px 0px 0px 20px;
color: #f9f9f9;
border: none;
line-height: 2em;
font-size: 2em;
}

.main
{
padding: 0px 12px;
margin: 12px 8px 8px 8px;
min-height: 420px;
}

.leftCol
{
padding: 6px 0px;
margin: 12px 8px 8px 8px;
width: 200px;
min-height: 200px;
}

.footer
{
color: #4e5766;
padding: 8px 0px 0px 0px;
margin: 0px auto;
text-align: center;
line-height: normal;
}

/* TAB MENU
----------------------------------------------------------*/

div.hideSkiplink
{
background-color:#3a4f63;
width:100%;
}

div.menu
{
padding: 4px 0px 4px 8px;
}

div.menu ul
{
list-style: none;
margin: 0px;
padding: 0px;
width: auto;
}

div.menu ul li a, div.menu ul li a:visited
{
background-color: #465c71;
border: 1px #4e667d solid;
color: #dde4ec;
display: block;
line-height: 1.35em;
padding: 4px 20px;
text-decoration: none;
white-space: nowrap;
}

div.menu ul li a:hover
{
background-color: #bfcbd6;
color: #465c71;
text-decoration: none;
}

div.menu ul li a:active
{
background-color: #465c71;
color: #cfdbe6;
text-decoration: none;
}

/* FORM ELEMENTS
----------------------------------------------------------*/

fieldset
{
margin: 1em 0px;
padding: 1em;
border: 1px solid #ccc;
}

fieldset p
{
margin: 2px 12px 10px 10px;
}

fieldset.login label, fieldset.register label, fieldset.changePassword label
{
display: block;
}

fieldset label.inline
{
display: inline;
}

legend
{
font-size: 1.1em;
font-weight: 600;
padding: 2px 4px 8px 4px;
}

input.textEntry
{
width: 320px;
border: 1px solid #ccc;
}

input.passwordEntry
{
width: 320px;
border: 1px solid #ccc;
}

div.accountInfo
{
width: 42%;
}

/* MISC
----------------------------------------------------------*/

.clear
{
clear: both;
}

.title
{
display: block;
float: left;
text-align: left;
width: auto;
}

.failureNotification
{
font-size: 1.2em;
color: Red;
}

.bold
{
font-weight: bold;
}

.submitButton
{
text-align: right;
padding-right: 10px;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s