gpt4 book ai didi

c# - 底层连接已关闭 : An unexpected error occurred on a send on

转载 作者:行者123 更新时间:2023-12-04 06:10:31 26 4
gpt4 key购买 nike

我有一个使用 VS 2010 用 [C#].Net 编写的 Windows 服务例程,它每天访问数据库一次,如果有任何记录需要处理。这些记录所有者将通过 Exchange 服务器收到一封电子邮件。
对于前几条记录,电子邮件发送正常,但在第 4 或第 5 封电子邮件之后,我收到错误消息

Service failed at :System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> 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.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.TlsStream.CallProcessAuthentication(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.WriteHeaders(Boolean async)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.Emit(HttpWebRequest& request)
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(HttpWebRequest& request)

如果我用断点调试例程,它工作正常。但是当我在没有任何断点的情况下运行它时,我得到了上述错误。
这是我的例程发送电子邮件的代码片段:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
lock (service)
{
if (service == null)
throw new Exception("service is null");
service.Credentials = new WebCredentials(data[0], data[1]);
service.Url = new Uri("https://owa.CompanyName.com/ews/exchange.asmx");
service.Timeout = 1200000;
EmailMessage message = new EmailMessage(service);
if (message == null)
throw new Exception("message is null");
message.Subject = "Test Email";
message.Body = "This is a Test EMail Please use the below link to update ";

//message.ToRecipients.Add(docList[0].CreatorEmailID);
message.ToRecipients.Add("EmailID@gmail.com");
for (int j = 0; j < emailList.Count; j++)
{
message.CcRecipients.Add("EmailID@gmail.com”);
}
System.Threading.Thread.Sleep(1000);
ServicePointManager.ServerCertificateValidationCallback += ignoreCertCallback;
try
{
message.Send();
}
finally
{
ServicePointManager.ServerCertificateValidationCallback -= ignoreCertCallback;
}
}

我在这里使用 Microsoft Exchange WebService 作为网络引用。
我试着用谷歌搜索它,但它并没有真正帮助我。如果有人能在这个问题上帮助我,我会非常高兴。

最佳答案

我猜:

Exchange 不喜欢您在短时间内打开的连接数量,并认为它受到攻击并随后阻止您建立更多连接。

我要做的第一件事就是重新编码那个“服务”。您不想为每条消息实例化一个新消息。只需重用现有的对象。此外,一次向它发送一条消息。

或者,与您的 Exchange 管理员联系,看看他们是否知道如何重新配置​​ Exchange 以让您的垃圾邮件,我的意思是非常重要的邮件,通过.. ;)

关于c# - 底层连接已关闭 : An unexpected error occurred on a send on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7838950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com