EWS Java 如何查找早于 xx 天的电子邮件并一次性全部删除

EWS Java how to find emails older than xx days and Delete all in one shot

我想在文件夹中查找早于 xx 天的项目,并一次性删除找到的所有项目。 我能够找到符合我的标准的项目。这是我的代码。

import org.joda.time.DateTime;
int purgeDays = 14;

    try {

        ItemView view = new ItemView(Integer.MAX_VALUE);

        Folder purgeFolder = Folder.bind(service, folderId);

        // need to convert to get Mon Sep 12 16:31:27 CDT 2016
        SearchFilter searchFilter = new SearchFilter.IsLessThanOrEqualTo(ItemSchema.DateTimeReceived, (DateTime.now().minusDays(purgeDays).toDate()));

        FindItemsResults<Item> emailsToPurge = service.findItems(purgeFolder.getId(), searchFilter, view);

        if (emailsToPurge != null && emailsToPurge.getItems() != null && emailsToPurge.getTotalCount() > 0 ) {

           // want something to delete all items at once
           emailsToPurge.deleteAll();

        } else {
            log.info("Found no emails to purge for Mailbox-"+ userName);
        }

    } catch (Exception e) {
        log.error("Exception "+ e.getMessage());
    }

查看 ExchangeService 上的 deleteItems 方法 class https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeservice.deleteitems(v=exchg.80).aspx 这允许您发送批量 DeleteItem 请求。我建议您分页删除,但一次不超过 1000 个项目,否则您可能会遇到节流问题 and/or 请求超时。