底层连接已关闭:在发送时发生意外错误
本文关键字:错误 意外 连接 | 更新日期: 2023-09-27 18:11:45
我有一个用[c#]编写的windows Service例程。. Net使用VS 2010,每天访问一次数据库,如果有任何记录需要处理。将通过Exchange服务器向这些记录所有者发送电子邮件。对于前几条记录,电子邮件发送得很好,但在第4或第5封电子邮件之后,我得到了一个错误,如show
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作为web参考。我试着在谷歌上搜索,但它并没有真正帮助我。如果有人能在这个问题上帮助我,我将非常高兴。
我猜:
Exchange不喜欢你在短时间内打开的连接数量,并认为它受到攻击,随后阻止你建立更多的连接。
我要做的第一件事是重新编码"服务"。您不希望为每条消息实例化一个新消息。只需重用现有的对象。还有,一次只发一条信息给它。
或者,和你的Exchange管理员谈谈,看看他们是否知道如何重新配置Exchange,让你的垃圾邮件,我的意思是非常重要的消息,通过…div;)
让我看看…
在有断点的调试器下工作,没有断点不能工作。
也许你需要限制你自己。
我有同样的问题,但我的代码是正确的调试器在本地主机上。当我上传我的应用程序,有人想要发送电子邮件这个错误出现。从传输流收到一个意外的EOF或0字节。
try
{
string txtBody = "";
string emailUser = ConfigurationManager.AppSettings["MailAuthUser"];
string emailPass = ConfigurationManager.AppSettings["MailAuthPass"];
string mailServer = ConfigurationManager.AppSettings["MailServer"];
txtBody = "<b>" + ("موضوع:" + " " + emailmod.Subject + "</b>" + "<br/><br/><b>" + "ارسال کننده: </b>" + emailmod.SenderEmail + "<br/><br/><b>" + "پیام: </b>" + emailmod.Message + "<br/><br/>" + emailmod.PagePath);
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
ExchangeService exservice = new ExchangeService();
AutodiscoverService auservice = new AutodiscoverService(mailServer);
if (auservice.ServerInfo != null)
{
try
{
exservice.AutodiscoverUrl(emailUser);
}
catch (AutodiscoverRemoteException ex)
{
//Console.WriteLine("Exception thrown: " + ex.Error.Message);
}
}
else
{
exservice.Url = new Uri("https://" + "outlook.apornak.com" + "/EWS/Exchange.asmx");
}
exservice.UseDefaultCredentials = true;
if (exservice.ServerInfo == null)
{
exservice.Credentials = new WebCredentials(emailUser, emailPass);
}
EmailMessage message = new EmailMessage(exservice);
message.Subject = "اعلام ایرادات و پیشنهادات";
message.Body = Resources.Message.AdvMailTemplate.Replace("#$Body$#", txtBody);
message.ToRecipients.Add("support@apornak.com");
message.From = "اعلام ایرادات و پیشنهادات";
message.ReplyTo.Add( emailmod.SenderEmail);
message.Save();
message.SendAndSaveCopy();
return Json(true);
}
catch (Exception)
{
{ return Json(false); }
throw;
}
通常情况下,如果存在证书问题,请在发出请求时使用
ServicePointManager。
ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);,
public static bool ValidateServerCertificate(对象发送者,x509证书证书,x509链链,SslPolicyErrors SslPolicyErrors){返回true;}
在IIS中,未选择ssl证书。也许是被提琴手的设置删除了…我用makecert命令重新创建了ssl证书,找到了!我在https://