Search code examples
c#exchange-serverexchangewebservicesexchange-server-2010exchange-server-2013

Delete emails from EWS ExchangeVersion.Exchange2013 vs 2010


My test code works with deleting emails using the ExchangeVersion.Exchange2010 as shown below.

        var finalsearchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And)
                {
                    new SearchFilter.IsGreaterThan(EmailMessageSchema.DateTimeReceived, DateTime.Now.AddDays(-15)),
                };

        view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);

        view.Traversal = ItemTraversal.Shallow;
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
        service.Credentials = new WebCredentials("user@example.com", "password", "domain");
        service.AutodiscoverUrl("user@example.com");
        FindItemsResults<Item> items = service.FindItems(new FolderId(WellKnownFolderName.Inbox, new Mailbox("mailboxName@example.com")), finalsearchFilter, view);

        if (items.Count() != 0)
        {
            IEnumerable<ItemId> itemIds = from p in items.Items select p.Id;
            service.DeleteItems(itemIds, DeleteMode.SoftDelete, null, null);
        }

But if i change the 2010 to 2013 I immediately get an error saying.

Microsoft.Exchange.WebServices.Data.ServiceVersionException : Exchange Server doesn't support the requested version.

Could someone point to me the right direction of what I am missing? When I looked into the msdn tutorials they still use the 2010 and sometimes the 2007 reference when calling an exchangeservice object. I've also seen other posts saying i need to specifically put the ExchangeVersion.Exchange2010 within but i already have it. I have also downloaded the latest nuget package exchange webservices if it helps, that's how my code could recognize that there's Exchange2013


Solution

  • This worked for me:

    item.Delete(DeleteMode.MoveToDeletedItems);
    

    where the item is the email