0

I'm trying to fetch all messages from a user-specified date on an Exchange 2016 server using the EWS managed API in C#.

I authenticate with:

public static void Login(string username, string password)
    {
        service.UseDefaultCredentials = false;
        service.Credentials = new WebCredentials(username, password);
        service.AutodiscoverUrl(username, RedirectionUrlValidationCallback);
    }

Then select the appropriate inbox with

sharedMailbox = new Mailbox(Properties.Settings.Default.Inbox);

the SMTP address is stored in Settings.settings .

I then find the desired folder using the following (from this thread):

        targetFolderId = new FolderId(WellKnownFolderName.Inbox, sharedMailbox);
        // set folder view

        view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
        view.PropertySet.Add(FolderSchema.DisplayName);
        view.Traversal = FolderTraversal.Deep;
        folderResults = service.FindFolders(WellKnownFolderName.Inbox, view);
        foreach(Folder f in folderResults)
        {
            if(f.DisplayName == "Invoices")
            {
                targetFolderId = f.Id;
                //tried showing a message box here
            }
        }

And use the following (filter code from here and retrieve details from Exchange server code from here) to get the messages I want:

public static void FetchUnreadMessages(DateTime searchDate)
    {

        SearchFilter greaterthanfilter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, searchDate);
        SearchFilter lessthanfilter = new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, searchDate.AddDays(1));
        SearchFilter dayFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterthanfilter, lessthanfilter);




        results = service.FindItems(targetFolderId, dayFilter, view);
        foreach(var item in results.Items)
        {
            emails.Add((EmailMessage)item);
        }



        PropertySet properties = (BasePropertySet.FirstClassProperties);
        service.LoadPropertiesForItems(emails, properties);



    }

I'm not sure where this is breaking down. I tried showing a message box in the foreach loop that finds the folder with the specified name, and it appears the folder is never found. I know there is a folder with that display name in the shared inbox.

I'm not great at debugging and unfortunately my grasp of the EWS API is pretty shaky. Any suggestions as to what I'm missing are welcome.

I keep everything related to the inbox in a static class so I only have to worry about one instance.

Nathan
  • 97
  • 2
  • 9

1 Answers1

0

There is some problems with you code first

Then select the appropriate inbox with sharedMailbox = new Mailbox(Properties.Settings.Default.Inbox); targetFolderId = new FolderId(WellKnownFolderName.Inbox, sharedMailbox);

Nothing wrong with this code but you probably need to understand what's happening here. This code doesn't make any calls to the server it just setups a FolderId class that you can use in a call to get one of the well known folder.

   view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
    view.PropertySet.Add(FolderSchema.DisplayName);
    view.Traversal = FolderTraversal.Deep;
    folderResults = service.FindFolders(WellKnownFolderName.Inbox, view);

This code would just search the Inbox Subfolder of the Mailbox who's credentials you are using. eg

service.Credentials = new WebCredentials(username, password);

If you wanted to Search the SubFolders of the Inbox of the SharedMailbox you would use

view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
view.PropertySet.Add(FolderSchema.DisplayName);
view.Traversal = FolderTraversal.Deep;
folderResults = service.FindFolders(targetFolderId , view);

because you are using the targetFolderId in the FindFolder operations that is telling Exchange to Search the Shared Mailbox rather then the mailbox associated with the credentials you are using.

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • Thanks for the tips - I actually noticed last night that I was searching the wrong inbox as you pointed out. The error I'm currently getting is from the following line: FindItemsResults fetchedMessages = service.FindItems(targetFolder, dayFilter, fetchView); Error: Microsoft.Exchange.WebServices.Data.ServiceResponseException: 'The request is invalid.' – Nathan Jun 07 '18 at 17:42
  • If you remove the SearchFilter does that work ? the logic in your search filter doesn't look correct to me. – Glen Scales Jun 07 '18 at 23:23