Download all email messages via EWS

若如初见. 提交于 2019-12-25 16:36:16

问题


I currently want to download all email messages (regardless in which folder they're in) to my SQL Server database.

Now while I know how to search for email messages or subscribe to streaming notifications, I've yet to learn on how to synchronize all messages from EWS to my database.

var emailMessages = GetItems<MSEmailMessage>(WellKnownFolderName.MsgFolderRoot);
foreach (var emailMessage in emailMessages)
{
    Debug.WriteLine(emailMessage.Subject);
}

private IList<T> GetItems<T>(WellKnownFolderName wellKnownFolderName) where T : Item
{
    IList<T> result = new List<T>();

    Folder folder = Folder.Bind(_exchangeService, wellKnownFolderName);
    if (folder.TotalCount > 0)
    {
        ItemView view = new ItemView(folder.TotalCount);
        FindItemsResults<Item> items = _exchangeService.FindItems(wellKnownFolderName, view);
        foreach (var resultItem in items.OfType<T>())
        {
            result.Add(resultItem);
        }
    }

    return result;
}

This returns 0 email messages (it even threw an exception before checking for the folder.TotalCount before initializing a new ItemView...).

While checking for WellKnownFolderName.Inbox returns the email messages from the inbox, it does not allow me to query for sub folders to synchronize the entirety of the messages.

What am I missing?


回答1:


You can build up a list of folders to search for mail in. Then iterate through each folder and get all the emails in that folder.

In the code snippet below we can create a folderSearchFilter with FolderTraversal set to Deep which will scan all sub folders of the target folder. We can then apply this filter to the two main well-known folders Inbox and SentItems

Once you have a list of folders to index, then you can use your own code to retrieve all the mails from that folder.

var view = new FolderView(int.MaxValue)
{
    PropertySet = new PropertySet(BasePropertySet.FirstClassProperties) { FolderSchema.DisplayName }
};
SearchFilter foldersearchFilter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);
view.Traversal = FolderTraversal.Deep;
List<Folder> searchFolders;
try
{
    searchFolders = new List<Folder>
            {
                Folder.Bind(ExchangeService, WellKnownFolderName.Inbox),
                Folder.Bind(ExchangeService, WellKnownFolderName.SentItems)
            };
}
catch (ServiceResponseException e) {}

searchFolders.AddRange(ExchangeService.FindFolders(WellKnownFolderName.Inbox, foldersearchFilter, view).Folders);
searchFolders.AddRange(ExchangeService.FindFolders(WellKnownFolderName.SentItems, foldersearchFilter, view).Folders);

var results = new List<Item>();
foreach (var searchFolder in searchFolders)
{
    //Get all emails in this folder
}



回答2:


Right, the root folder likely has 0 messages in it. When you do FindItems in a folder, the results don't bubble up from subfolders. You need to iterate over every folder if you want to get to their messages.



来源:https://stackoverflow.com/questions/28670593/download-all-email-messages-via-ews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!