1

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

ANiceSunset
  • 63
  • 3
  • 12
  • Are you sure you are actually talking to a 2013 server? – Jan Doggen Feb 01 '16 at 10:01
  • That is what I figured when I first looked into it. If i take the majority of my code out and leave with just the "exchangeService" object, credentials and autodiscoverUrl, everything is fine. No errors. But simply adding the "findItems" part I get that error. Is it because I'm not actually talking to a 2013 server until the FindItems part? – ANiceSunset Feb 01 '16 at 17:52
  • I don't use C#, but I have just tested shooting a SOAP request to Exchange Server 2010 with ``, and this gives me the error *The specified server version is invalid.* It seems that you cannot call ES with a version number that is too high. I develop my code for version Exchange2007SP1 as a lowest common denominator. – Jan Doggen Feb 02 '16 at 07:22
  • I can understand MS Examples using 'old' versions in their sample documentation for two reasons: 1) Old examples were never updated 2) It shows the version a feature was first implemented. – Jan Doggen Feb 02 '16 at 07:23
  • So i looked into the server i'm calling, and you're right. I learned that the FindItems method returns a call from EWS. I checked what version it is and it's Exchange 2010_SP2. By removing the finditems part, i'm not making a call to EWS, that's why i didn't get an error. Because i'm calling and requesting a 2013, the version number is too high. I'm working with 2010 at the moment now. Thanks for the reminder. – ANiceSunset Feb 03 '16 at 18:37

1 Answers1

2

This worked for me:

item.Delete(DeleteMode.MoveToDeletedItems);

where the item is the email

  • 1
    Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.[How to Answer](https://stackoverflow.com/help/how-to-answer) – Elletlar Nov 22 '18 at 15:43