5,692,513 members and growing! (16,149 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate

Handy ASP.NET Email Control

By Trevor Swanepoel

An email feedback control written in VB.NET
VB, Windows, .NET 1.0, .NET, Visual Studio, Dev

Posted: 11 Apr 2003
Updated: 28 Apr 2003
Views: 95,300
Bookmarked: 20 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 3.19 Rating: 3.78 out of 5
1 vote, 14.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
3 votes, 42.9%
4
3 votes, 42.9%
5

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)> _ 'No extra linebreak after this!!

        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 Labels 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"
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"
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"
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)> _ 'No extra linebreak after this!!

        Public MustInherit Class MailAnything
        Inherits System.Web.UI.UserControl
        'Add your button so that it doesn't need to be done in the aspx side

        Protected WithEvents btnMail As Button
        Protected lblSent As Label
        'Declare this public string so that the "mail to" address can be set
'in the aspx page
Public Mailto As String #Region " Web Form Designer Generated Code " #End Region Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load 'Empty End Sub Private Sub RegisterBtn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnMail.Click 'Set random counter Dim iCount As Integer = 0 'Set control counter Dim iControlCount As Integer 'Dim filename in case there are files to email Dim sFileNameOnly As String Dim sControl As String Dim sBody As StringBuilder = New StringBuilder() 'Count the controls iControlCount = Controls.Count 'Loop through each control to add to message body Do While iCount < (iControlCount - 1) 'do not include "lblSent" in the body If Not Controls(iCount).ID = "lblSent" Then Select Case Controls(iCount).GetType().Name Case "HtmlInputFile" 'If there is a file to email then save the file
'to disk
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), _
"\") sFileNameOnly = Mid(sFileNamePath, _
(Len(sFileNamePath) - iFileNameLength) + 2) currentControl.PostedFile.SaveAs(_
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 />", _
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 'Increment random counter iCount = iCount + 1 Loop
'Dim mail message Dim mailCurrent As New MailMessage() 'Assign mail attributes mailCurrent.Body = sBody.ToString() mailCurrent.To = Mailto mailCurrent.From = Mailto mailCurrent.BodyFormat = MailFormat.Html 'If there is a file to mail add it to the email If Len(sFileNameOnly) > 0 Then mailCurrent.Attachments.Add(_
New System.Web.Mail.MailAttachment(_
Server.MapPath("
\MailTS\uploaded\" & sFileNameOnly))) End If 'Set SMTP server to localhost SmtpMail.SmtpServer = "localhost" 'Send mail SmtpMail.Send(mailCurrent) 'Assign text to say mail was sent lblSent.Text = "Your mail was sent to: " & Mailto End Sub 'Some extra formatting 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Trevor Swanepoel



Location: South Africa South Africa

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralMaster PagesmemberWingnut121312:20 20 Jun '06  
GeneralGood job !memberMohammed Aziz Elnahrawi22:52 19 Dec '05  
GeneralHELPsusscookie king7:08 23 Aug '05  
GeneralWeb form control : TablesussAnonymous6:31 13 Dec '04  
GeneralObject ErrorsussChianti7310:05 10 Dec '03  
GeneralSome project files would be nicememberSteven Carleton11:48 6 Jul '03  
GeneralHow to use this inside a control?sussAnonymous10:51 19 May '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Apr 2003
Editor: Heath Stewart
Copyright 2003 by Trevor Swanepoel
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project