【发布时间】:2014-12-02 17:24:10
【问题描述】:
代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtfrom.Text, "OLMS");
msg.To.Add(new MailAddress(txtto.Text));
msg.Subject = txtsub.Text;
msg.Body = txtbody.Text;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new NetworkCredential(txtfrom.Text, txtto.Text);
smtp.EnableSsl = true;
smtp.Send(msg);
lblresult.Text = "Message Sent Successful!";
}
}
堆栈跟踪:
[SocketException (0x2746): An existing connection was forcibly closed by the remote host]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size,
SocketFlags socketFlags) +6416371
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +130
[IOException: Unable to read data from the transport connection:
An existing connection was forcibly closed by the remote host.]
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,
Int32 size) +296
System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32
count) +45
System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset,
Int32 count) +106
System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader
caller, Boolean oneLine) +203
System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader
caller) +16
System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String&
response) +54
System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn) +28
System.Net.Mail.SmtpConnection.GetConnection(ServicePoint
servicePoint) +843
System.Net.Mail.SmtpTransport.GetConnection(ServicePoint
servicePoint) +170
System.Net.Mail.SmtpClient.GetConnection() +44
System.Net.Mail.SmtpClient.Send(MailMessage message) +1554
[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage message) +1906
_Default.Button1_Click(Object sender, EventArgs e) in c:\Users
\Azhar Khan\Documents\Visual Studio 2010\WebSites
\WebSite2\Default.aspx.cs:31
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9553594
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.R
aisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
我还没有为 SMTP 配置 IIS(我不知道是否要这样做)。我也没有在这个网站的 Web.config 文件中添加任何 smtp 配置。关于我收到的异常的任何帮助都会阻止我发送电子邮件。谢谢
【问题讨论】:
-
将
Smtp.Port设置为587有帮助吗?由于防火墙规则,我无法从这里对其进行测试。 -
感谢 PeterK 的回复。不,将端口设置为 587 不起作用。它引发异常“SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证”。顺便说一下,我为用于发送电子邮件的电子邮件设置了一个强密码。
-
稍微好一点 :) 但是,我在您的代码中看不到任何错误。 Gmail设置怎么样?见stackoverflow.com/questions/17227532/…
-
PeterK 我的电子邮件没有指出任何可疑的登录尝试。这是否意味着我登录时使用的凭据不正确,考虑到上述异常...?
-
我在这个问题上找到的每一个资源都指向启用不太安全的应用程序google.com/settings/security/lesssecureapps -- 你试过了吗?