【问题标题】:How to fix "System.IO.IOException: Unable to read data from the transport connection: The connection was closed" in ASP.NET?如何修复 ASP.NET 中的“System.IO.IOException:无法从传输连接读取数据:连接已关闭”?
【发布时间】:2020-11-03 02:15:17
【问题描述】:

我正在尝试从 ASP.NET Core 3.1 应用程序发送邮件,但我总是收到此错误消息。我该如何解决这个问题?

[我找到了一个与此类似的answer,但答案是 9 岁,对我没有帮助。]

// Exception e;
// e.Message + e.InnerException + e.GetBaseException() =
Failure sending mail. 
System.IO.IOException: Unable to read data from the transport connection: 
The connection was closed. at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, 
Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader 
caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) 
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) 
at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) 
at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) System.IO.IOException: Unable to read data from the transport connection: The connection was closed. 
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) 
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at 
System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at 
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at 
System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)

这是我的邮件发送代码 -

private void sendmail(string usermail, string message)
{
    SmtpClient smtpClient = new SmtpClient("smtp.yandex.com", 465);
    smtpClient.Credentials = new System.Net.NetworkCredential("may mail address", "password");
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.EnableSsl = true;
    MailMessage mail = new MailMessage();
    mail.Body = message;
    mail.Subject = "My Awesome Subject";
    mail.From = new MailAddress("may mail address", "Awesome User");
    mail.To.Add(new MailAddress(usermail));
    smtpClient.Send(mail);
}

【问题讨论】:

  • 如果你尝试使用端口 587 而不是 465 会怎样?

标签: c# asp.net .net email smtp


【解决方案1】:

Yandex 有一个用于外部应用程序的特殊密码,因此请在页面https://passport.yandex.ru/profile 上创建该密码。第二点是使用了错误的端口,很奇怪,但是Yandex使用的是587而不是465。

【讨论】:

  • 嗨!,现在我遇到了一种新型错误 - System.Net.Mail.SmtpException:命令序列错误。服务器响应为:5.5.4 错误:首先发送 AUTH 命令。我试图关注 - stackoverflow.com/questions/42391432/… 但仍然收到错误消息。我的密码是正确的。并尝试使用应用密码。
  • 尝试将服务器 URL 更改为 ru 相关站点:smtp.yandex.ru
  • 这很奇怪,因为我复制了您的代码并更改了 URL 和凭据,并且一切正常。也许你有一些网络问题?
  • 没有遇到任何其他与互联网相关的问题,因此网络非常稳定。我会再试一次,然后回复你。
【解决方案2】:

您需要更改以下两件事,它应该可以工作:

  1. 根据answer:尝试使用端口 587 而不是 465。端口 465 在技术上已被弃用。 SmtpClient smtpClient = new SmtpClient("smtp.yandex.com", 587);

  2. 由于您使用 SSL,您需要添加:System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;。欲了解更多信息,请查看answer

System.Net.ServicePointManager.SecurityProtocol - 此属性 选择安全套接字层 (SSL) 或传输的版本 用于新连接的层安全 (TLS) 协议 仅安全超文本传输​​协议 (HTTPS) 方案;现存的 连接没有改变。

【讨论】:

  • 嗨!,现在我遇到了一种新型错误 - System.Net.Mail.SmtpException:命令序列错误。服务器响应为:5.5.4 错误:首先发送 AUTH 命令。我试图关注 - stackoverflow.com/questions/42391432/… 但仍然收到错误消息。我的密码是正确的。
猜你喜欢
  • 2015-01-15
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 2023-01-11
  • 1970-01-01
  • 2012-07-04
相关资源
最近更新 更多