我正在努力让这个联系表单正常工作,但我收到一条错误消息:“发送消息失败”。问题是页面底部附近的代码部分,它执行这行代码:
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 = "myemail@email.com"
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 team@acupuncturepainpros.com.
</asp:View>
</asp:MultiView>
答案 0 :(得分:2)
我完全复制了您的代码并用它将消息发送到我自己的邮件服务器。这看起来像是您要发送给它的服务器的问题。
您是否尝试在没有正确身份验证/规则等的情况下通过smtp服务器进行中继?
请记住,对于大多数电子邮件服务器,如果您要将电子邮件发送到他们不负责的地址,您需要进行身份验证并获得通过该身份验证进行中继的授权。
-
好吧,您可以随时查看SMTP服务器日志(如果您有权访问它们),以确定邮件无法发送的原因。
否则,您可以从运行应用程序的同一台计算机上“手动”向服务器发送电子邮件。这可以告诉你问题是什么。
所以,你这样做:
在您的应用程序托管的同一台机器上(并假设我们在这里处理Windows)通过运行以下命令Telnet到邮件服务器:
telnet smtp.siteA.com 25
如果你正在运行Win7并且不想打开telnet,则可以使用PuTTY代替。
连接到服务器后,您应该看到某种欢迎消息。然后,键入以下命令,每行一行,以Enter结束每行。
EHLO example.com
MAIL FROM: <example@example.com>
RCPT TO: <admin@siteA.com>
DATA
Subject:Test Subject
Test Body
.
QUIT
应该通过服务器的确认来响应这些命令中的每一个。如果其中一个出现问题,那么你会被告知。
如果对第一行(其中包含EHLO的那一行)的响应有问题,请尝试:
HELO example.com
我已经使用example.com来表示电子邮件来自的域名和地址。这可以改变为你喜欢的任何东西,通常对结果无关紧要。
这整个过程模拟了当您的代码尝试发送电子邮件时将发生的完全相同的过程。如果您使用相同的变量(如服务器和地址)从同一台计算机上运行,那么您应该得到相同的结果。