question

Manny-9339 avatar image
0 Votes"
Manny-9339 asked GlenScales-6756 commented

how read email from Exhcange Server?

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?

office-exchange-server-connectivity
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Manny-9339 ,

Welcome to the Microsoft Q&A forum and thanks for posting your questions here, but sorry that EWS managed API and other Scripts related issues are not supported.
But I think you could get help from Glen, he is an expert in this field.

Sincerely hope you could solver your problem soon.

Best regards,
Lou

0 Votes 0 ·

1 Answer

GlenScales-6756 avatar image
0 Votes"
GlenScales-6756 answered GlenScales-6756 commented

ServiceRequestException

This means its an issue that your client has talking with the Exchange Server, transient connectivity issues are pretty common for a number of reasons (both client,server or topology) the first thing would be to use a try catch block and retry the request. You can check the server logs also to see what was happening at that time eg the server can drop the connection if its too busy to process it etc.

· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @GlenScales-6756
This block of code is inside the try catch, I compile it in a Windows Service and when I install it it works fine for a day or two and then it starts throwing that error in the log file.
That does not allow me to think what else could be happening, because first if I can read the emails and then out of nowhere it fails.

Thank you.

0 Votes 0 ·

Does it continue to the throw the error until you make some sort of intervention or does your code recover after some time ? From the information you have given the only thing i can say is that it client connectivity related as there is no response from the server. So it either a problem on the client, within the client network path (firewall etc), or possibly the server just isn't accepting connection at the time of the error (patching etc).

0 Votes 0 ·

Thank you for you comments @GlenScales-6756

The code not recover after of message of error, the problem is exactly on the line 17 from example attached so first I need to be sure that client work correctly.

0 Votes 0 ·
Show more comments