问题描述
我目前想将所有电子邮件(无论它们位于哪个文件夹中)下载到我的SQL Server数据库中.
I currently want to download all email messages (regardless in which folder they're in) to my SQL Server database.
现在,尽管我知道如何搜索电子邮件或订阅流式通知,但我还没有学习如何将所有消息从EWS同步到我的数据库.
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;
}
这将返回0封电子邮件(在初始化新的ItemView
...之前,甚至在检查folder.TotalCount
之前引发了异常.)
This returns 0 email messages (it even threw an exception before checking for the folder.TotalCount
before initializing a new ItemView
...).
在检查WellKnownFolderName.Inbox时,会从收件箱中返回电子邮件,但不允许我查询子文件夹来同步整个消息.
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.
我想念什么?
推荐答案
您可以建立一个文件夹列表来搜索邮件.然后遍历每个文件夹并获取该文件夹中的所有电子邮件.
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.
在下面的代码片段中,我们可以创建一个folderSearchFilter
,并将FolderTraversal
设置为Deep,它将扫描目标文件夹的所有子文件夹.然后,我们可以将此过滤器应用于两个主要的知名文件夹Inbox
和SentItems
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
}
这篇关于通过EWS下载所有电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!