2

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

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 发送消息时也会发生同样的事情。

4

2 回答 2

3

知道了!

本质上,当您成功连接到服务器但随后要求它做一些它不具备做的事情时,似乎会发生此错误(ErrorCode 10054)。

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

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

通常当我看到它是 Exchange 服务器上的配置时。在我们的环境中,我们的网络管理员必须明确允许机器将 Exchange 服务器用作中继。如果运行我的代码的机器没有显式添加到服务器,我会收到此消息。希望这会有所帮助。

于 2009-07-21T19:48:46.220 回答