【问题标题】:sending email. Asp.net. C#. Existing connection was forcibly closed by the remote host发送电子邮件。 ASP.NET。 C#。现有连接被远程主机强行关闭
【发布时间】: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 -- 你试过了吗?

标签: c# asp.net email


【解决方案1】:

确保在https://google.com/settings/security/lesssecureapps 的 Google 帐户设置中启用允许安全性较低的应用程序选项 -- 从 2014 年下半年开始,这是使用基本身份验证通过 Gmail SMTP 进行身份验证所必需的。

或者,您可以禁用上述选项并为 SMTP 身份验证实施 XOAUTH2(请参阅https://developers.google.com/gmail/xoauth2_protocol)。

【讨论】:

    【解决方案2】:

    从您的代码看来,您正在通过smtp.EnableSsl = true; 启用 SSL。 如果您想以相同的方式进行操作,请使用 465 端口。 以下是相关问题的已解决答案链接:

    How can I send emails through SSL SMTP with the .NET Framework?

    如果您发现任何困难,请告诉我。

    【讨论】:

    • 你提到的链接我试过了,但它对我不起作用。通过给定链接中的上述方法,它抛出异常“消息无法发送到 SMTP 服务器。传输错误代码为 0x80040217。服务器响应不可用”
    猜你喜欢
    • 2010-11-12
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多