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


<center>
        <fieldset style="padding: 10px;">
            <legend style="margin-top: -10px;">Please enter your information: </legend>
            <table border="0">
                <tr>
                    <td>
                        <b>Your Name:</b>
                    </td>
                    <td>
                        <br />
                        <asp:textbox ID="txtName" runat="server" EmptyMessage="Please input your name."
                            Skin="Default" Width="200px" CausesValidation="True">
                        </asp:textbox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="*"
                            ControlToValidate="txtName"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Your Email:</b>
                    </td>
                    <td>
                        <asp:textbox ID="txtEmail" runat="server" EmptyMessage="Please input your email address."
                            Width="200px" CausesValidation="True">
                        </asp:textbox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                            ControlToValidate="txtEmail"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <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>
                </tr>
                <tr>
                    <td colspan="2" align="left">
                        <fieldset>
                            <legend>Please select the departments you commonly work with: </legend>
                            <asp:CheckBoxList ID="txtCheckBox" runat="server" RepeatColumns="4">
                                <asp:ListItem>One</asp:ListItem>
                                <asp:ListItem Text="Two" />
                                <asp:ListItem Text="Three" />
                                <asp:ListItem Text="Four" />
                                <asp:ListItem Text="Five" />
                                <asp:ListItem Text="Six" />
                                <asp:ListItem>Seven</asp:ListItem>
                                <asp:ListItem>Eight</asp:ListItem>
                                <asp:ListItem>Nine</asp:ListItem>
                                <asp:ListItem>Ten</asp:ListItem>
                            </asp:CheckBoxList>
                        </fieldset>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <b>Comments:</b>
                        <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">
                        </asp:textbox>
                    </td>
                </tr>
                <tr>
                    <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" />
                    </td>
                </tr>
            </table>
        </fieldset>
    </center>


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
        '!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
        Const ToAddress As String = "tthomas@ocalafl.org"

        '(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
        Next

        'This is extra code that can be used or deleted for the checkboxlist
        'If strchklist = "" Then
        '    Response.Write("No item Selected")
        'Else
        '    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

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

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

            ' 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’


  <system.net>
    <mailSettings>
      <smtp>
        <network
             host="mail.yourserver.com"
             port="25"
             userName="username "
             password="password"
             />
      </smtp>
    </mailSettings>
  </system.net>

Advertisements

~ 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:

WordPress.com Logo

You are commenting using your WordPress.com 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: