EWS FindItems Welknownfolder inbox randomly null

Rick Doll 21 Reputation points
2022-04-07T13:34:15.853+00:00

This driving me bonkers.

I am trying to read all mail in the inbox of a O365 account, iterate the items, do logic, move the item to another folder when done.

Randomly when i try and run it, it the finditems gives me a null exception.

I can't for the life of me figure what the deal is here.

Any pointers on what I could be doing wrong here would be greatly appreciated...

            var cca = ConfidentialClientApplicationBuilder
                    .Create(clientId)
                    .WithClientSecret(clientSecret)
                    .WithTenantId(tenantId)
                    .Build();

            var scopes = new[] { "https://outlook.office365.com/.default" };

            var authResult = cca.AcquireTokenForClient(scopes)
                         .ExecuteAsync().Result;

            Service = new ExchangeService
            {
                Credentials = new OAuthCredentials(authResult.AccessToken),
                Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"),

                ImpersonatedUserId =
                    new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "emailaccount"),

            };

            Service.HttpHeaders.Add("X-AnchorMailbox", "emailaccount");

 var mail = Service.FindItems(WellKnownFolderName.Inbox, new ItemView(1000));

I then loop the mail, do my logic, then move it..

 Folder rootfolder = Folder.Bind(Service, WellKnownFolderName.MsgFolderRoot);
                            rootfolder.Load();
                            Folder foundFolder = rootfolder.FindFolders(new FolderView(100)).FirstOrDefault(x => x.DisplayName == type.MailMove);

                            item.Move(foundFolder.Id);
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
508 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Glen Scales 4,431 Reputation points
    2022-04-08T00:02:39.397+00:00

    I would suggest you enable tracing in your code https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-trace-requests-responses-to-troubleshoot-ews-managed-api-applications and look at the result of the trace when you receive the error. If you don't have any code to handle throttling and your moving a lot of messages (one message at a time) then its mostly likely you will get throttled at some point. If you want your code to be robust you should be really expecting that some EWS requests will fail and need to retried (eg the mailbox maybe being moved etc, throttled, transient 500 errors ). Also this

    Folder foundFolder = rootfolder.FindFolders(new FolderView(100)).FirstOrDefault(x => x.DisplayName == type.MailMove);

    Should really be a search

    var view = new FolderView(1);      
    var filter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, type.MailMove);  
    var results = Service.FindFolders(WellKnownFolderName.Root, filter, view);  
    

    But the trace is the best place to start as you can correlated what is actually happening in the server response when you receive the error

    0 comments No comments