ASP.NET Simple Email Form

I was needing to create an email form and ended up finding some other issues with sending the values from checkboxlists. This was taken from several different sites, it should work with no troubles. I also added a try/catch statement to catch errors. If you have any questions, don’t hesitate to contact me. thx.

The form html code in the contactform.aspx

        <fieldset style="padding: 10px;">
            <legend style="margin-top: -10px;">Please enter your information: </legend>
            <table border="0">
                        <b>Your Name:</b>
                        <br />
                        <asp:textbox ID="txtName" runat="server" EmptyMessage="Please input your name."
                            Skin="Default" Width="200px" CausesValidation="True">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="*"
                        <b>Your Email:</b>
                        <asp:textbox ID="txtEmail" runat="server" EmptyMessage="Please input your email address."
                            Width="200px" CausesValidation="True">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                    <td colspan="2">
                        Your email address will ONLY be used to configure your account and respond appropriately.
                        <br />
                        It WILL NOT be sold, or used to send you spam.
                        <hr />
                    <td colspan="2" align="left">
                            <legend>Please select the departments you commonly work with: </legend>
                            <asp:CheckBoxList ID="txtCheckBox" runat="server" RepeatColumns="4">
                                <asp:ListItem Text="Two" />
                                <asp:ListItem Text="Three" />
                                <asp:ListItem Text="Four" />
                                <asp:ListItem Text="Five" />
                                <asp:ListItem Text="Six" />
                    <td colspan="2">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
                            ControlToValidate="txtComments"></asp:RequiredFieldValidator><br />
                        <asp:textbox ID="txtComments" TextMode="MultiLine" runat="server" Width="400px"
                            Height="200px" EmptyMessage="Please tell us how we can assist you!" CausesValidation="True">
                    <td colspan="2" align="center">
                        <asp:Label ID="lblMessage" runat="server" Text="" ForeColor="Green"></asp:Label>
                        <asp:Button runat="server" ID="SendEmail" Text="Send Feedback" />

My entire VB Codebehind for contactform.aspx.vb

Partial Class accessrequest
    Inherits System.Web.UI.Page

    Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click
        Const ToAddress As String = ""

        '(1) Create the MailMessage instance
        Dim mm As New Net.Mail.MailMessage(txtEmail.Text, ToAddress)

        Dim strchklist As String = ""
        Dim li As ListItem

        For Each li In txtCheckBox.Items()
            If li.Selected Then
                strchklist += li.Text + " <br /> "
            End If

        'This is extra code that can be used or deleted for the checkboxlist
        'If strchklist = "" Then
        '    Response.Write("No item Selected")
        '    Response.Write(("You selected : " + strchklist))
        'End If

        '(2) Assign the MailMessage's properties
        mm.Subject = "Website Form: File Management Site New User Request Submitted."
        mm.Body = "<h2>Your Heading for Email</h2> Customer's Name: " & txtName.Text & "<br />Customer's Email: " & txtEmail.Text & "<br />Customer's Comments:" & txtComments.Text & "<br /> Customer's Selections: " & txtCheckBox.SelectedItem.Selected

        mm.IsBodyHtml = True

            '(3) Create the SmtpClient object
            Dim smtp As New Net.Mail.SmtpClient

            '(4) Send the MailMessage (will use the Web.config settings)

            ' show message on screen that email has been sent
            lblMessage.ForeColor = System.Drawing.Color.Green
            lblMessage.Text = "<strong>Email Sent SucessFully</strong><br><hr>"

            ' display error message
        Catch ex As Exception
            lblMessage.ForeColor = System.Drawing.Color.Red
            lblMessage.Text = "There was a problem sending the email, the error is: <br />" & ex.Message & "<br><hr>"
        End Try

    End Sub

End Class

In your webconfig add this under ‘configuration’

             userName="username "


~ by worldcrossings on June 22, 2010.

One Response to “ASP.NET Simple Email Form”

  1. […] ASP.NET Simple Email Form « Worldcrossings's Blog […]

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: