0
votes

just started on a blank visual studio installation writing a new ews application.

Everything went fine, i tested it with an testmail ive sended from my new application an everything works great.

Next step should have been to retrieve two test-mails from my inbox via the "findItems" method, but when i'm trying this, i get always a timeout.

i dont understand it, because sending e-mails works but retrieving gives a timeout. does anybody can help out here?

thank you

this is the code i use right now:

    {
        static void Main(string[] args)
        {
            
            ExchangeService service = new ExchangeService();
            service.Credentials = new WebCredentials("user", "password");
            service.Url = new Uri("https://web.mydomain.com/EWS/Exchange.asmx");

            if (service != null)
            {

                ItemView view = new ItemView(10);
                view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);
                SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "test");
                
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);

                Console.WriteLine(findResults.TotalCount);
            }

        }

    }```
2
sorry, this is the error message: Microsoft.Exchange.WebServices.Data.ServiceRequestException: "The request failed. Timeout für Vorgang überschritten"f1ssl
next info: sending an email and try to save a copy gets me an timeout aswell, but the mails are going out to the recipient. EmailMessage message = new EmailMessage(service); message.Subject = "Interesting"; message.Body = "The proposition has been considered."; message.ToRecipients.Add("[email protected]"); message.SendAndSaveCopy();f1ssl

2 Answers

0
votes

With a Search like your using on a Mailbox folder that has a very large item count a timeout wouldn't be unexpected. I would suggest you include a Time Restriction at least in your filter eg

        SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "test");
        SearchFilter DateRestrication = new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, DateTime.Now.AddDays(-7));
        SearchFilter sfCol = new SearchFilter.SearchFilterCollection(LogicalOperator.And) { searchFilter, DateRestrication };

        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sfCol, view);

or better using AQS/KQL

       FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, "Subject:test", view);
0
votes

Thanks Glen, for your answer!

I wonder about the timeout because there are only 2 mails inside the inbox of the targeteted mailbox.

I tried your changes, but get the same error. I think it must be something with the communication with the server in general. maybe another port for incoming mails is used that the port i use, when i want to send an email?

this is, what the trace said:

`

<Trace Tag="EwsRequestHttpHeaders" Tid="1" Time="2020-08-27 07:24:58Z">
POST /EWS/Exchange.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Accept: text/xml
User-Agent: ExchangeServicesClient/15.00.0847.030
Accept-Encoding: gzip,deflate


</Trace>
<Trace Tag="EwsRequest" Tid="1" Time="2020-08-27 07:24:58Z" Version="15.00.0847.030">
  <?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2013_SP1" />
    </soap:Header>
    <soap:Body>
      <m:FindItem Traversal="Shallow">
        <m:ItemShape>
          <t:BaseShape>IdOnly</t:BaseShape>
          <t:AdditionalProperties>
            <t:FieldURI FieldURI="item:Subject" />
          </t:AdditionalProperties>
        </m:ItemShape>
        <m:IndexedPageItemView MaxEntriesReturned="10" Offset="0" BasePoint="Beginning" />
        <m:ParentFolderIds>
          <t:DistinguishedFolderId Id="inbox" />
        </m:ParentFolderIds>
        <m:QueryString>Subject:test</m:QueryString>
      </m:FindItem>
    </soap:Body>
  </soap:Envelope>
</Trace>