ASP.NET(VB) 联系邮件程序表单错误:MailObj.Send(myMessage) 执行失败。

发布于 2024-11-25 02:39:17 字数 7665 浏览 0 评论 0原文

我试图让此联系表单正常工作,但收到一条错误消息:“发送消息失败”。问题出在页面底部附近执行这行代码的代码部分:

   MailObj.Send(myMessage)

我已将虚拟凭据放入下面的代码片段中,但对我的 smtp un/pw 进行了三重检查,但无法弄清楚。

VB 代码

Imports System.Web.Mail
Imports System.Net.Mail

Partial Class Contact
    Inherits System.Web.UI.Page

    Protected Sub SubmitForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsValid Then Exit Sub

        Dim SendResultsTo As String = "[email protected]"
        Dim smtpMailServer As String = "stmp.mysite.com"
        Dim smtpUsername As String = "myusername"
        Dim smtpPassword As String = "mypassword"
        Dim MailSubject As String = "Customer Message"

        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

           'problem occurs here.  The error details state "Failure to send mail"
            MailObj.Send(myMessage)

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

    End Sub
End Class

ASP.NET 代码

  <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>
                <%--make sure they enter an email--%>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Email"
                    Display="Dynamic" ErrorMessage="Please enter your email address." SetFocusOnError="True"
                    CssClass="ValidateMessage" ForeColor="">* Required</asp:RequiredFieldValidator>
                <%--<make sure its a valid email--%>
                <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="Message">
                    Please type your message below:
                    <%--make sure user enters a message--%>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="Message"
                        ErrorMessage="Please enter a message" SetFocusOnError="True" CssClass="ValidateMessage"
                        ForeColor="">* Required</asp:RequiredFieldValidator>
                    <br />
                    <%-- text box that users can type their message--%>
                    <asp:TextBox ID="Message" runat="server" TextMode="MultiLine" Columns="55" Rows="10">
                    </asp:TextBox>
                </label>
                <br />
               <% %>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
                    ShowSummary="True" CssClass="ValidateMessage" ForeColor="" />
                <br />
                <br />
                <%-- submit button--%>
                <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. One of our dedicated staff
                members will contact you shortly.<br />
            </asp:View>
            <asp:View ID="FormErrorMessage" runat="server">
                We're sorry, there was an error sending your message. Please give us a call at 1-877-302-5604
                or email us at [email protected].
            </asp:View>
        </asp:MultiView>

I am trying to get this contact form to work, but I get an error message saying: "failed to send message". The problem is the section of code near the bottom of the page where it executes this line of code:

   MailObj.Send(myMessage)

I've put dummy credentials in the snippet below but triple-checked my smtp un/pw and cant figure things out.

VB CODE

Imports System.Web.Mail
Imports System.Net.Mail

Partial Class Contact
    Inherits System.Web.UI.Page

    Protected Sub SubmitForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsValid Then Exit Sub

        Dim SendResultsTo As String = "[email protected]"
        Dim smtpMailServer As String = "stmp.mysite.com"
        Dim smtpUsername As String = "myusername"
        Dim smtpPassword As String = "mypassword"
        Dim MailSubject As String = "Customer Message"

        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

           'problem occurs here.  The error details state "Failure to send mail"
            MailObj.Send(myMessage)

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

    End Sub
End Class

ASP.NET CODE

  <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>
                <%--make sure they enter an email--%>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Email"
                    Display="Dynamic" ErrorMessage="Please enter your email address." SetFocusOnError="True"
                    CssClass="ValidateMessage" ForeColor="">* Required</asp:RequiredFieldValidator>
                <%--<make sure its a valid email--%>
                <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="Message">
                    Please type your message below:
                    <%--make sure user enters a message--%>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="Message"
                        ErrorMessage="Please enter a message" SetFocusOnError="True" CssClass="ValidateMessage"
                        ForeColor="">* Required</asp:RequiredFieldValidator>
                    <br />
                    <%-- text box that users can type their message--%>
                    <asp:TextBox ID="Message" runat="server" TextMode="MultiLine" Columns="55" Rows="10">
                    </asp:TextBox>
                </label>
                <br />
               <% %>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
                    ShowSummary="True" CssClass="ValidateMessage" ForeColor="" />
                <br />
                <br />
                <%-- submit button--%>
                <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. One of our dedicated staff
                members will contact you shortly.<br />
            </asp:View>
            <asp:View ID="FormErrorMessage" runat="server">
                We're sorry, there was an error sending your message. Please give us a call at 1-877-302-5604
                or email us at [email protected].
            </asp:View>
        </asp:MultiView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2024-12-02 02:39:17

我完全复制了您的代码并用它向我自己的邮件服务器发送消息。这看起来像是您将其发送到的服务器的问题。

您是否尝试在没有正确身份验证/规则等的情况下通过 smtp 服务器进行中继?

请记住,对于大多数电子邮件服务器,如果您想将电子邮件发送到他们不负责的地址,您需要进行身份验证并有权通过该身份验证进行中继。

--

嗯,您可以随时查看 SMTP 服务器日志(如果您有权访问它们),以确定邮件发送失败的原因。

否则,您可以从运行应用程序的同一台计算机“手动”向服务器发送电子邮件。这可以告诉您问题是什么。

因此,您可以这样做:

从您的应用程序所在的同一台计算机(假设我们在这里处理 Windows)通过运行以下命令 Telnet 到邮件服务器:

telnet smtp.siteA.com 25

如果您运行的是 Win7 并且不想转向在 telnet 上,您可以使用 PuTTY 代替。

连接到服务器后,您应该会看到某种欢迎消息。然后,每行键入以下命令,并以 Enter 结尾。

EHLO example.com
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
DATA
Subject:Test Subject
Test Body
.
QUIT

每个命令都应该得到服务器的确认响应。如果其中一个有问题,那么您也会被告知。

如果对第一行(其中包含 EHLO 的行)的响应有问题,请尝试:

HELO example.com

我使用 example.com 来表示电子邮件来源的域和地址。可以将其更改为您喜欢的任何内容,并且通常对结果没有太大影响。

这整个过程模拟了当您的代码尝试发送电子邮件时将发生的完全相同的过程。如果您在具有相同变量(例如服务器和地址)的同一台计算机上运行,​​那么您应该得到相同的结果。

I exactly copied your code and used it to send a message to my own mail server. This looks like an issue with the server you're sending it to.

Are your trying to relay through an smtp server without the proper authentication/rules etc?

Remember, with most email servers, if you want to send email to an address they are not responsible for, you need to authenticate and have authorization to relay via that authentication.

--

Well, you can always look in the SMTP server logs (if you have access to them) in order to determine why you're message is failing to send.

Otherwise you can "manually" send an email to the server from the same machine your app is run from. This can tell you what the problem is.

So, you do this:

From the same machine your app is hosted at (and assuming we're dealing with windows here) Telnet to the mail server by running the following command:

telnet smtp.siteA.com 25

If you're running Win7 and don't want to turn on telnet you can use PuTTY instead.

Once connected to the server you should see a welcome message of some sort. Then, type the following commands one per line ending each line with Enter.

EHLO example.com
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
DATA
Subject:Test Subject
Test Body
.
QUIT

Each of those commands should be responded to with an acknowledgement by the server. If there is a problem with one of them then you will be told as much.

If the response to the first line (the one with EHLO in it) has a problem then try:

HELO example.com

I've used example.com to represent a domain and address that the email is coming from by the way. That can be changed to whatever you like and generally won't matter all that much to the result.

This whole thing simulates the exact same process that will be happen when your code tries to send the email. If you're running from the same machine with the same variables such as server and address, then you should get the same results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文