I have this code snippet but after a time it marks an error:
exchange = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
exchange.Credentials = new NetworkCredential("username", "password", "na.empresa.com");
exchange.Url = new Uri("https://mail.empresa.com/EWS/Exchange.asmx");
exchange.PreAuthenticate = true;
exchange.TraceEnabled = true;
WriteToFile(DateTime.Now + " Connected to Exchange Server : " + exchange.Url.Host);
SearchFilter.SearchFilterCollection findAllResult = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
WriteToFile(DateTime.Now + " Start read mail");
if (exchange != null)
{
FindItemsResults<Item> findResults = exchange.FindItems(WellKnownFolderName.Inbox, findAllResult, new ItemView(100));
if (findResults.TotalCount <= 0)
{
WriteToFile(DateTime.Now + " No new Emails found!!");
return;
}
}
This is the error message:
5/27/2021 4:19:48 AM Error Connecting to Exchange Server!!
Microsoft.Exchange.WebServices.Data.ServiceRequestException:
The request failed. The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
---> System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
---> System.IO.IOException: Unable to read data from the transport connection:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
How I can solution it?