I've needed to include a feedback form for my web site. Using the SmtpClient class to put together a simple feedback form is easy.
First, you need to design your asp.net form. I've only include a short fragment that contains the necessary controls:
<form id="form1" runat="server">
<div>
<table id="Layout">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Subject:"></asp:Label>
</td>
<td>
<asp:TextBox ID="Subject" runat="server" Width="496px" Height="21px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="E-mail:"></asp:Label>
</td>
<td>
<asp:TextBox ID="Email" runat="server" Width="496px" Height="21px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Message:"></asp:Label>
</td>
<td>
<asp:TextBox ID="Message" runat="server" Width="496px" Height="186px"
BorderStyle="Solid" BorderWidth="1px" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="butSend" runat="server" Text="Send" onclick="butSend_Click" />
</td>
</tr>
</table>
</div>
</form>
The c# code is fairly simple:
using System;
using System.Collections;
using System.Configuration;
using System.Web.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using System.IO;
using System.Xml;
public partial class Feedback : System.Web.UI.Page
{
string btnGoBack = "<br/><br/><input id=\"btnBack\" type=\"button\" value=\"Back\" onclick=\"history.back()\"/>";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void butSend_Click(object sender, EventArgs e)
{
try
{
Configuration AccessRequestConfig = WebConfigurationManager.OpenWebConfiguration("/sandbox");
KeyValueConfigurationElement mailServerName = AccessRequestConfig.AppSettings.Settings["ServerName"];
KeyValueConfigurationElement mailFrom = AccessRequestConfig.AppSettings.Settings["From"];
KeyValueConfigurationElement mailTo = AccessRequestConfig.AppSettings.Settings["To"];
KeyValueConfigurationElement mailSubject = AccessRequestConfig.AppSettings.Settings["Subject"];
KeyValueConfigurationElement mailServerAccount = AccessRequestConfig.AppSettings.Settings["Account"];
KeyValueConfigurationElement mailServerPassword = AccessRequestConfig.AppSettings.Settings["Password"];
string body = this.Message.Text + "\n";
body += this.Email.Text;
// MailMessage is used to represent the e-mail being sent
using (MailMessage message =
new MailMessage())
{
string test = mailFrom.Value;
message.From = new MailAddress(test);
message.To.Add(mailTo.Value);
message.Subject = mailSubject.Value;
message.Body = body;
// SmtpClient is used to send the e-mail
SmtpClient mailClient = new SmtpClient(mailServerName.Value);
// We'll need to setup a standard account to use for sending.
mailClient.Credentials = new System.Net.NetworkCredential(mailServerAccount.Value, mailServerPassword.Value);
// Send delivers the message to the mail server
mailClient.Send(message);
}
// let the user know he/she was successful
string resMsg = "" +
"Your request has been submitted, click the 'Back' button to return to the website." + btnGoBack;
this.form1.InnerHtml = resMsg + "";
}
catch (FormatException ex)
{
this.form1.InnerHtml = "" +
ex.Message + "
Your request was not submitted due to a Format exception." + btnGoBack;
}
catch (SmtpException ex)
{
string msg = ex.Message + "\n";
if (null != ex.InnerException)
msg += ex.InnerException.Message;
this.form1.InnerHtml = "" +
msg + "
Your request was not submitted, there was a problem connecting to the SMTP server." + btnGoBack;
}
finally
{
Session.Clear();
Session.Abandon();
}
}
}
The settings in my web.config file are as follows:
<appSettings>
<add key="ServerName" value="your.mailserver.here" />
<add key="Account" value="youraccount@namehere" />
<add key="Password" value="yourpasswordhere" />
<add key="From" value="addresstoshowinfromfield" />
<add key="To" value="wheretosend@goeshere" />
<add key="Subject" value="Feedback" />
</appSettings>