.net - 无法通过 Exchange 发送电子邮件 : An existing connection was forcibly closed by the remote host

标签 .net smtp exchange-server system.net.mail

我刚刚注意到我的代码中的一些电子邮件操作失败了,我正在尝试修复它,但是我不断收到以下错误:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
 ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   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)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

我的代码相当简单:

MailMessage mail = new MailMessage();
mail.To.Add(email_to);
mail.From = new MailAddress(email_from);
mail.Subject = email_subject;
mail.Body = email_body;


var client = new SmtpClient(smtp_server_url);
client.Credentials = new NetworkCredential(username, password, domain);
client.Timeout = 30000;
client.Send(mail);

有人知道会发生什么吗?我们的交换服务器管理员说她在日志中看不到任何内容。

另外,这并不是一个惊喜,但当我尝试从 powershell 发送消息时,也会发生同样的事情。

最佳答案

明白了!

本质上,当您成功连接到服务器但随后要求它执行它不具备执行的操作时,似乎会发生此错误(错误代码 10054)。

就我而言,发生的事情是这样的:

  • 我曾经通过我们的前端邮件服务器 mail.mycompany.com 发送邮件
  • 但这不是 Exchange 服务器的地址,而是您将浏览器指向的用于访问 Outlook Online 的地址。
  • 这在以前工作得很好,因为对该地址的非网络请求被路由到交换服务器。在某些时候,这项政策发生了变化 - 在我们的例子中,因为垃圾邮件管理已移交给 AT&T。当然我没有被告知任何这些。
  • 由于确实有一台服务器在该站点上运行,它会让我连接到它,但然后立即断开连接(或者甚至可能不允许它完全打开)
  • 设置正确的主机名解决了问题。

关于.net - 无法通过 Exchange 发送电子邮件 : An existing connection was forcibly closed by the remote host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1161253/

相关文章:

java - MS 交换日历和 Java 应用程序之间的双向同步

exchange-server - 如何连接到多个 Exchange 服务器?

php - 使用 PHP 的 mail() 函数需要 SMTP 服务器吗

java - 如何在开发过程中阻止 java 应用程序发送电子邮件?

c# - 带有代码修复的 VS2015 诊断 - NuGet 或 VSIX 或两者?

c# - NetStandard/Net Framework 引用 hell

email - 警告 : mail(mail. 日志):无法打开流:权限被拒绝

java - 有没有办法通过 API 在 MS Exchange Server 中创建帐户

asp.net - DropDownList 的 SelectedValue 与 SelectedItem.Value

c# - 无法从 asp.net 网站项目中的 app_code 文件夹访问类