我試圖讓這個聯繫表單正常工作,但我收到一條錯誤消息:「無法發送消息」。問題是附近的頁面底部的代碼在那裏執行這行代碼的部分: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>
拋出異常的InnerException屬性是否提供了附加信息? – hatchet
這是你希望我們看的很多代碼。你確定你不能縮小這個範圍嗎? –
是的...對不起,我猜我的帖子的一部分被誤切了...控制檯報告是「Fail Send Message」 – MonkeyJones