我有一个用[C#]编写的Windows服务程序。使用VS 2010的网络,每天一次点击数据库,如果有任何记录需要解决。这些记录所有者将通过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作为Web引用。 我试过谷歌搜索它,但它并没有真正帮助我。如果有人能在这个问题上帮助我,我会很高兴。
答案 0 :(得分:2)
我的猜测:
Exchange不喜欢您在短时间内打开的连接数量,并认为它受到攻击并随后阻止您建立更多连接。
我要做的第一件事就是重新编写“服务”。您不希望为每条消息实例化一个新消息。只需重用现有对象即可。另外,一次发送一条消息。
或者,请与您的Exchange管理员联系,了解他们是否知道如何通过以下方式重新配置交换,以便让您的垃圾邮件(我的意思是非常重要的消息)通过..;)
答案 1 :(得分:1)
让我们看看......
在带有断点的调试器下工作,没有。
也许你需要限制自己。
答案 2 :(得分:0)
我有同样的问题,但我的代码在localhost上的调试器下是正确的。当我上传我的应用程序,有人想发送电子邮件时出现此错误。 从传输流中收到意外的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;
}
答案 3 :(得分:0)
通常,如果存在证书问题,请使用此处进行重新申请,
ServicePointManager.ServerCertificateValidationCallback + = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
...和
public static bool ValidateServerCertificate(对象发件人,X509Certificate证书,X509Chain链,SslPolicyErrors sslPolicyErrors) { 返回true; }
答案 4 :(得分:0)
在IIS中,未选择ssl证书。或许被Fiddler设置删除了...... 我用makecert命令和eureka重新创建了ssl证书! 在https://
上调用Web服务时出现此错误