Avatar of rgn2121
rgn2121Flag for United States of America asked on

Please offer advice/help with my contact form.

I have created a simple contact form, but wanted to get some advice from more experienced programmers as to what if anything else I need to make sure that I add for security, validation, user experience, etc.  I have posted my aspx and I will add my code behind below it.  
The only thing that doesn't work, other than just getting suggestions from you all, is that I what the items in the body to be on different lines.  I tried adding the system.environment.newline (I do mostly windows programming) and I tried adding a "</br>" between each item.

I will distribute points evenly and fairly as possible, of course, one person might be able to answer everything effectively.

Thanks for the help...just trying to learn more about best practices...
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="Contact._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            width: 40%;
            border-style: solid;
            border-width: 1px;
            background-color: #E9F2F7;
        }
        .style2
        {
            width: 71px;
        }
        .style3
        {
            width: 246px;
        }
        .style4
        {
            width: 57px;
        }
        .style5
        {
            width: 60px;
        }
        .style7
        {
            height: 20px;
        }
        .style9
        {
            width: 246px;
            height: 23px;
        }
        .style10
        {
            width: 57px;
            height: 23px;
        }
        .style11
        {
            width: 71px;
            height: 23px;
        }
        .style12
        {
            width: 71px;
            height: 20px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table cellpadding="8" class="style1">
        <tr>
            <td align="right" class="style2">
                Name:</td>
            <td class="style3" colspan="2">
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            </td>
            <td class="style4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="txtEmail" ErrorMessage="Please enter your name.">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td align="right" class="style2">
                Email:</td>
            <td class="style3" colspan="2">
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
            </td>
            <td class="style4">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="txtEmail" ErrorMessage="Please enter your email.">*</asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                    ControlToValidate="txtEmail" 
                    ErrorMessage="Please enter a vaild email address." 
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td align="right" class="style2">
                Phone:</td>
            <td class="style3" colspan="2">
                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
            </td>
            <td class="style4">
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                Fax:</td>
            <td class="style3" colspan="2">
                <asp:TextBox ID="txtFax" runat="server"></asp:TextBox>
            </td>
            <td class="style4">
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                Company:</td>
            <td class="style3" colspan="2">
                <asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>
            </td>
            <td class="style4">
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                Comments:</td>
            <td class="style3" colspan="2">
                <asp:TextBox ID="txtComments" runat="server" Height="84px" TextMode="MultiLine" 
                    Width="235px"></asp:TextBox>
            </td>
            <td class="style4">
                <asp:CustomValidator ID="CustomValidator1" runat="server" 
                    ControlToValidate="txtComments" 
                    ErrorMessage="Comments are limited to 200 characters or less.">*</asp:CustomValidator>
            </td>
        </tr>
        <tr>
            <td align="right" class="style11">
                Priority:</td>
            <td class="style9" colspan="2">
                <asp:DropDownList ID="ddlPriority" runat="server">
                    <asp:ListItem Selected="True">Select...</asp:ListItem>
                    <asp:ListItem>Normal</asp:ListItem>
                    <asp:ListItem>Medium</asp:ListItem>
                    <asp:ListItem>High</asp:ListItem>
                </asp:DropDownList>
            </td>
            <td class="style10">
                </td>
        </tr>
        <tr>
            <td class="style12">
                </td>
            <td colspan="3" class="style7">
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" Height="41px" />
            </td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;</td>
            <td class="style5">
                <asp:Button ID="btnClear" runat="server" Text="Clear" />
            </td>
            <td class="style3">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
            </td>
            <td class="style4">
                &nbsp;</td>
        </tr>
    </table>
    </form>
</body>
</html>

Open in new window

.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
rgn2121

8/22/2022 - Mon
ASKER
rgn2121

Here is the .vb file...
Imports System
Imports System.Net.Mail
 
Partial Public Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    End Sub
 
    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        If txtComments.Text.Length > 200 Then
            args.IsValid = False
        Else
            args.IsValid = True
 
        End If
    End Sub
 
    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
 
'Reset form controls???
    End Sub
 
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        Const ToAddress As String = "me@someserver.com"
        Const SMTPServer As String = "smtp@someserver.com"
 
        '(1) Create the MailMessage instance
        Dim MailMsg As New MailMessage(txtEmail.Text, ToAddress)
 
        '(2) Assign the MailMessage's properties
        MailMsg.Subject = "Contact Feedback"
        MailMsg.Body = txtName.Text & txtPhone.Text & txtFax.Text & _
                       txtFax.Text & Environment.NewLine & txtComments.Text
        MailMsg.IsBodyHtml = True
 
        '(3) Create the SmtpClient object
        Dim smtp As New SmtpClient
        smtp.Host = SMTPServer
 
        '(4) Send the MailMessage (will use the Web.config settings)
        smtp.Send(MailMsg)
 
        'Response.Redirect("")
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Gary Davis

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
rgn2121

Thanks....anything else I should do.  I can't believe that I have do the best I could programming wise...
Gary Davis

You set smtp.Host = SMTPServer but SMTPServer is an email address and not a server. You can try "localhost" which should work if you have an SMTP server on your web server or else you will have to specify some external server that is running an SMTP server (listening on port 25).

Gary Davis
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
rgn2121

Yeah...I actually have it as an SMTP server in the real file.  I was going through and changing things to take out specifics and did that...

Anything else?