2011-07-16 30 views
1

我試圖讓這個聯繫表單正常工作,但我收到一條錯誤消息:「無法發送消息」。問題是附近的頁面底部的代碼在那裏執行這行代碼的部分:ASP.NET(VB)聯繫郵件表單錯誤:MailObj.Send(myMessage)執行失敗。

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> 
+0

拋出異常的InnerException屬性是否提供了附加信息? – hatchet

+0

這是你希望我們看的很多代碼。你確定你不能縮小這個範圍嗎? –

+0

是的...對不起,我猜我的帖子的一部分被誤切了...控制檯報告是「Fail Send Message」 – MonkeyJones

回答

2

我精確複製你的代碼,並用它來發送消息到我自己的郵件服務器。這看起來像是您要發送給它的服務器的問題。

你是否試圖通過一個smtp服務器沒有適當的身份驗證/規則等?

請記住,對於大多數電子郵件服務器,如果您想將電子郵件發送到他們不負責的地址,您需要進行身份驗證並通過該身份驗證進行授權。

-

好了,你可以隨時看在SMTP服務器日誌(如果你有機會到他們),以確定爲什麼你的消息未能發送。

否則,您可以「手動」從運行應用程序的同一臺計算機向服務器發送電子郵件。這可以告訴你問題是什麼。

所以,你這樣做:

從您的應用程序是在(並假設我們正在與這裏的窗戶處理)通過運行以下命令遠程登錄到郵件服務器託管在同一臺機器:

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代表域和地址的電子郵件來了順便說一句。這可以改變爲任何你喜歡的東西,對結果來說通常都不會有太大影響。

這整個事情模擬了當您的代碼嘗試發送電子郵件時將發生的完全相同的過程。如果你從具有相同變量(如服務器和地址)的同一臺計算機上運行,​​那麼你應該得到相同的結果。

+0

如果我的收件人是[email protected]而stmp是stmp.site @ .com,該怎麼辦?不應該那樣工作? – MonkeyJones

+0

我假設你的意思是你通過smtp服務器smtp.siteA.com向[email protected]發送一封電子郵件。如果是這種情況,那麼通常你會希望服務器負責該地址,在這種情況下,你不會中繼。當我說「如果你想發送電子郵件到他們不負責的地址」時,這就是我的意思。如果您嘗試發送到服務器負責的地址,則還有其他可能性。中繼只是人們常見的問題。你確定你發送的郵箱存在嗎?那也會導致它失敗。 –

+0

感謝您的洞察力的人。我非常感謝所有社區的幫助。你說得對,我把它發送到一個電子郵件地址(我已經檢查過...它的工作原理)@ siteA.com通過stmp服務器stmp.siteA.com。但它仍然沒有工作...任何想法,我應該檢查哪裏/我還有什麼...我真的難住 – MonkeyJones

相關問題