Introduction
Most websites have the need for some sort of "feedback form" or "email-us" page, which, as we know, is tedious to re-develop on every site, even in .NET. I have developed this simple E-Mail control in VB.NET that can be wrapped around any form to be turned into an email form, even with file inputs.
The first major thing to overcome is the <ParseChildren(False)>
property which must be set just above the CLASS in the .ascx.vb file (no extra linebreaks). This allows the User Control to access the controls on the .aspx page where it is used.
<ParseChildren(False)> _
Public MustInherit Class MailAnything
Inherits System.Web.UI.UserControl
If you do have a file input on your web form remember to set the enctype="multipart/form-data"
.
Another important thing to remember is to use Web Forms Label
s to name your fields on the web form so that it can be read by your user control and form part of the email.
The basic elements of the form in the .aspx file is as follows (example).
<Bluegrass:Mailer Mailto="you@you.com" runat="server">
<table border="0" ID="Table1">
<tr>
<td align="right" valign="top">
<asp:Label ID="lblFirstname" Runat="server" />
</td>
<td>
<asp:TextBox CssClass="input" ID="txtFirstname" Runat="server" />
</td>
</tr>
<tr>
<td align="right" valign="top">
<asp:Label ID="lblLastname" Runat="server" />
</td>
<td>
<asp:TextBox CssClass="input" ID="txtLastname" Runat="server" />
</td>
</tr>
<tr>
<td align="right" valign="top">
<asp:Label ID="lblDescription" Runat="server" />
</td>
<td>
<asp:TextBox CssClass="input" ID="txtDescription" TextMode="MultiLine"<BR> Runat="server" />
</td>
</tr>
<tr>
<td align="right" valign="top">
<asp:Label ID="lblAge" Runat="server" />
</td>
<td>
<asp:DropDownList CssClass="input" ID="selAge" Runat="server" />
</td>
</tr>
<tr>
<td align="right" valign="top">
<asp:Label ID="lblfile" Runat="server" />
</td>
<td>
<input CssClass="input" type="file" id="FileInput" runat="server"<BR> NAME="FileInput">
</td>
</tr>
<tr>
<td align="right" valign="top">
<asp:Label ID="lblReply" Runat="server" />
</td>
<td>
<asp:CheckBox ID="chkReply" Runat="server" />
</td>
</tr>
</table>
</Bluegrass:Mailer>
Also don't forget to register your User control in the .aspx file
<%@ Register TagPrefix="Bluegrass" TagName="Mailer"<BR> Src="controls/MailAnything.ascx" %>
The rest is then all done in the user control which can be re-used in any project. Here is how I did the user control:
Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.Web.Mail
Imports System.Text
Namespace Bluegrass.Mail
<ParseChildren(False)> _
Public MustInherit Class MailAnything
Inherits System.Web.UI.UserControl
Protected WithEvents btnMail As Button
Protected lblSent As Label
Public Mailto As String
#Region " Web Form Designer Generated Code "
End Region
Private Sub Page_Load(ByVal sender As System.Object, _<BR> ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub RegisterBtn_Click(ByVal sender As System.Object, _<BR> ByVal e As System.EventArgs) Handles btnMail.Click
Dim iCount As Integer = 0
Dim iControlCount As Integer
Dim sFileNameOnly As String
Dim sControl As String
Dim sBody As StringBuilder = New StringBuilder()
iControlCount = Controls.Count
Do While iCount < (iControlCount - 1)
If Not Controls(iCount).ID = "lblSent" Then
Select Case Controls(iCount).GetType().Name
Case "HtmlInputFile"
Dim currentControl As HtmlInputFile = Controls(iCount)
If Len(currentControl.PostedFile.FileName) > 0 Then
Dim iFileNameLength As Integer
Dim PostedFile = currentControl.PostedFile
Dim sFileNamePath As String
sFileNamePath = currentControl.PostedFile.FileName
iFileNameLength = InStr(1, StrReverse(sFileNamePath), _<BR> "\")
sFileNameOnly = Mid(sFileNamePath, _<BR> (Len(sFileNamePath) - iFileNameLength) + 2)
currentControl.PostedFile.SaveAs(_<BR> Server.MapPath("\MailTS\uploaded\" & sFileNameOnly))
End If
Case "TextBox"
Dim currentControl As TextBox = Controls(iCount)
sBody.AppendFormat("{0} <br />", currentControl.Text)
Case "Label"
Dim currentControl As Label = Controls(iCount)
sBody.AppendFormat("{0} ", currentControl.Text)
Case "DropDownList"
Dim currentControl As DropDownList = Controls(iCount)
sBody.AppendFormat("{0} <br />", _<BR> currentControl.SelectedItem.Text)
Case "CheckBox"
Dim currentControl As CheckBox = Controls(iCount)
If currentControl.Checked Then
sBody.AppendFormat("{0} <br />", "Yes")
Else
sBody.AppendFormat("{0} <br />", "No")
End If
End Select
End If
iCount = iCount + 1
Loop<BR>
Dim mailCurrent As New MailMessage()
mailCurrent.Body = sBody.ToString()
mailCurrent.To = Mailto
mailCurrent.From = Mailto
mailCurrent.BodyFormat = MailFormat.Html
If Len(sFileNameOnly) > 0 Then
mailCurrent.Attachments.Add(_<BR> New System.Web.Mail.MailAttachment(_<BR> Server.MapPath("\MailTS\uploaded\" & sFileNameOnly)))
End If
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(mailCurrent)
lblSent.Text = "Your mail was sent to: " & Mailto
End Sub
Protected Overrides Sub RenderChildren(ByVal output As HtmlTextWriter)
If HasControls() Then
Dim iCount As Integer = 3
Dim iControlCount As Integer
iControlCount = Controls.Count
Do While iCount < (iControlCount - 1)
Controls(iCount).RenderControl(output)
iCount = iCount + 1
Loop
Controls(0).RenderControl(output)
Controls(1).RenderControl(output)
Controls(2).RenderControl(output)
End If
End Sub
End Class
End Namespace
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.