邮件表单的问题

时间:2011-11-30 14:14:44

标签: asp.net vb.net

我正在尝试启动并运行邮件表单。我在网上找到了一个代码生成器,所以我希望这很容易。但我没有收到错误消息:“由于技术问题,您的消息可能尚未发送。”

如果有人可以查看我的代码以查看可能缺少的内容,我们将非常感激。

提前致谢。

<script runat="server">
    Protected Sub SubmitForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsValid Then Exit Sub

        Dim SendResultsTo As String = "DELETED"
        Dim smtpMailServer As String = "DELETED"
        Dim smtpUsername As String = "DELETED"
        Dim smtpPassword As String = "DELETED"
        Dim MailSubject As String = "Form Results"

        Try
            Dim txtQ As TextBox = Me.FormContent.FindControl("TextBoxQ")
            If txtQ IsNot Nothing Then
                Dim ans As String = ViewState("hf1")
                If ans.ToLower <> txtQ.Text.ToLower Or ans.ToUpper <> txtQ.Text.ToUpper Then
                    Me.YourForm.ActiveViewIndex = 3
                    Exit Sub
                End If
            End If

            Dim FromEmail As String = SendResultsTo
            Dim msgBody As StringBuilder = New StringBuilder()
            Dim sendCC As Boolean = False


            For Each c As Control In Me.FormContent.Controls
                Select Case c.GetType.ToString
                    Case "System.Web.UI.WebControls.TextBox"
                        Dim txt As TextBox = CType(c, TextBox)
                        If txt.ID.ToLower <> "textboxq" Then
                            msgBody.Append(txt.ID & ": " & txt.Text & vbCrLf & vbCrLf)
                        End If
                        If txt.ID.ToLower = "email" Then
                            FromEmail = txt.Text
                        End If
                        If txt.ID.ToLower = "subject" Then
                            MailSubject = txt.Text
                        End If
                    Case "System.Web.UI.WebControls.CheckBox"
                        Dim chk As CheckBox = CType(c, CheckBox)
                        If chk.ID.ToLower = "checkboxcc" Then
                            If chk.Checked Then sendCC = True
                        Else
                            msgBody.Append(chk.ID & ": " & chk.Checked & vbCrLf & vbCrLf)
                        End If

                    Case "System.Web.UI.WebControls.RadioButton"
                        Dim rad As RadioButton = CType(c, RadioButton)
                        msgBody.Append(rad.ID & ": " & rad.Checked & vbCrLf & vbCrLf)
                    Case "System.Web.UI.WebControls.DropDownList"
                        Dim ddl As DropDownList = CType(c, DropDownList)
                        msgBody.Append(ddl.ID & ": " & ddl.SelectedValue & vbCrLf & vbCrLf)
                End Select
            Next
            msgBody.AppendLine()

            msgBody.Append("Browser: " & Request.UserAgent & vbCrLf & vbCrLf)
            msgBody.Append("IP Address: " & Request.UserHostAddress & vbCrLf & vbCrLf)
            msgBody.Append("Server Date & Time: " & DateTime.Now & vbCrLf & vbCrLf)

            Dim myMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
            myMessage.To.Add(SendResultsTo)
            myMessage.From = New System.Net.Mail.MailAddress(FromEmail)
            myMessage.Subject = MailSubject
            myMessage.Body = msgBody.ToString
            myMessage.IsBodyHtml = False
            If sendCC Then myMessage.CC.Add(FromEmail)


            Dim basicAuthenticationInfo As New System.Net.NetworkCredential(smtpUsername, smtpPassword)
            Dim MailObj As New System.Net.Mail.SmtpClient(smtpMailServer)
            MailObj.Credentials = basicAuthenticationInfo
            MailObj.Send(myMessage)

            Me.YourForm.ActiveViewIndex = 1
        Catch
            Me.YourForm.ActiveViewIndex = 2
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            Dim lbl As Label = Me.FormContent.FindControl("labelq")
            If lbl IsNot Nothing Then
                Dim rq(3) As String
                rq(0) = "Is fire hot or cold?"
                rq(1) = "Is ice hot or cold?"
                rq(2) = "Is water wet or dry?"

                Dim ra(3) As String
                ra(0) = "hot"
                ra(1) = "cold"
                ra(2) = "wet"

                Dim rnd As New Random
                Dim rn As Integer = rnd.Next(0, 3)
                lbl.Text = rq(rn)
                ViewState("hf1") = ra(rn)
            End If
        End If
    End Sub
</script>

<form runat="server">
             <asp:MultiView ID="YourForm" runat="server" ActiveViewIndex="0">
        <asp:View ID="FormContent" runat="server">
                    <label for="Email">
                Enter your Email Address:<br />
                <asp:TextBox ID="Email" runat="server" Columns="35">
                </asp:TextBox>
            </label>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Email"
                Display="Dynamic" ErrorMessage="Please enter your email address." SetFocusOnError="True"
                CssClass="ValidateMessage" ForeColor="">* Required</asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="Email"
                ErrorMessage="Please enter a valid email address." SetFocusOnError="True" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                CssClass="ValidateMessage" ForeColor="">* Please enter a valid email address.</asp:RegularExpressionValidator>
            <br />
            <br />
                    <label for="First_Name">
                First Name:<br />
                <asp:TextBox ID="First_Name" runat="server" Columns="35"></asp:TextBox>
            </label>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="First_Name"
                ErrorMessage="Please enter your first name." SetFocusOnError="True" CssClass="ValidateMessage"
                ForeColor="">* Required</asp:RequiredFieldValidator>
            <br />
            <br />
                    <label for="Last_Name">
                Last Name:<br />
                <asp:TextBox ID="Last_Name" runat="server" Columns="35">
                </asp:TextBox>
            </label>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="Last_Name"
                ErrorMessage="Please enter your last name." SetFocusOnError="True" CssClass="ValidateMessage"
                ForeColor="">* Required</asp:RequiredFieldValidator>
            <br />
            <br />
                    <label for="Organization">
                Organization:<br />
                <asp:TextBox ID="Organization" runat="server" Columns="35">
                </asp:TextBox>
            </label>
            <asp:RequiredFieldValidator ID="RequiredFieldValidatorOrganization" runat="server" ControlToValidate="Organization"
                ErrorMessage="Please enter your organization." SetFocusOnError="True" CssClass="ValidateMessage"
                ForeColor="">* Required</asp:RequiredFieldValidator>
            <br />
            <br />
                    <label for="Website">
                Website:<br />
                <asp:TextBox ID="website" runat="server" Columns="40">
                </asp:TextBox>
            </label>
            <br />
            <br />
                    <label for="Street">
                Street:<br />
                <asp:TextBox ID="Street" runat="server" Columns="60">
                </asp:TextBox>
            </label>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="Street"
                ErrorMessage="Please enter your street." SetFocusOnError="True" CssClass="ValidateMessage"
                ForeColor="">* Required</asp:RequiredFieldValidator>
            <br />
            <br />
                    <label for="City">
                City:<br />
                <asp:TextBox ID="City" runat="server" Columns="60">
                </asp:TextBox>
            </label>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="City"
                ErrorMessage="Please enter your city." SetFocusOnError="True" CssClass="ValidateMessage"
                ForeColor="">* Required</asp:RequiredFieldValidator>
            <br />
            <br />
                    <label for="Zip">
                Zip/Postal Code:<br />
                <asp:TextBox ID="Zip" runat="server" Columns="40"></asp:TextBox>
            </label>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="Zip"
                Display="Dynamic" ErrorMessage="Please enter your zip/postal code." SetFocusOnError="True"
                CssClass="ValidateMessage" ForeColor="">* Required</asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="Zip"
                ErrorMessage="Please enter a valid U.S. zip code." ValidationExpression="\d{5}(-\d{4})?"
                SetFocusOnError="True" CssClass="ValidateMessage" ForeColor="">* Please enter a valid U.S. zip code.</asp:RegularExpressionValidator>
            <br />
            <br />
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
                ShowSummary="False" CssClass="ValidateMessage" ForeColor="" />
            <br />
            <br />
            <asp:CheckBox ID="CheckBoxCC" runat="server" Text="Send me a carbon copy of this email." />
            <br />
            <br />
            <asp:Button ID="SubmitForm" runat="server" OnClick="SubmitForm_Click" Text="Submit Form" />
            <br />
        </asp:View>
        <asp:View ID="FormConfirmationMessage" runat="server">
            Your message has been sent. Thank you for contacting us.<br />
        </asp:View>
        <asp:View ID="FormErrorMessage" runat="server">
            Due to technical difficulty, your message may NOT have been sent.
        </asp:View>
        <asp:View ID="FormSpamMessage" runat="server">
            You did not correctly answer the anti-spam question. Please go back and try again.</asp:View>
    </asp:MultiView>


   </form>

0 个答案:

没有答案