Skip to content

Instantly share code, notes, and snippets.

@mdmsua
Created January 20, 2020 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdmsua/602d5f604dfe241331dd998ba42463c2 to your computer and use it in GitHub Desktop.
Save mdmsua/602d5f604dfe241331dd998ba42463c2 to your computer and use it in GitHub Desktop.
EWS Inbox Cleanup
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Exchange.WebServices.Data;
using static Microsoft.Exchange.WebServices.Data.SearchFilter;
namespace Inboxer
{
class MainClass
{
public static void Main()
{
var service = new ExchangeService { Credentials = new WebCredentials("username", "password"), Url = new Uri("https://owa.check24.de/ews/exchange.asmx") };
IList<ItemId> items = default;
do
{
items = service.FindItems(WellKnownFolderName.Inbox, new ContainsSubstring(ItemSchema.Subject, "[PRTG Network Monitor]", ContainmentMode.Prefixed, ComparisonMode.Exact), new ItemView(byte.MaxValue) { PropertySet = PropertySet.FirstClassProperties })?.Select(item => item.Id).ToList();
if (items?.Count > 0)
{
Console.WriteLine($"Removing {items?.Count} items...");
service.DeleteItems(items, DeleteMode.HardDelete, default, default);
Console.WriteLine("Done");
}
}
while (items?.Count > 0);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment