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