简体   繁体   English

从Exchange Web服务托管API获取收件箱中的所有邮件,并将其存储为.eml文件

[英]Fetching all mails in Inbox from Exchange Web Services Managed API and storing them as a .eml files

I want to fetch all mails in the Inbox folder using EWS Managed API and store them as .eml . 我想使用EWS托管API获取收件箱文件夹中的所有邮件,并将它们存储为.eml The problem is in fetching (1) all mails with (2) all headers (like from, to, subject) (I am keeping information of those values of from , to and other properties somewhere else, so I need them too) and (3)byte[] EmailMessage.MimeContent.Content . 问题在于获取(1)所有邮件的所有邮件(例如from,to,subject) (我在其他地方保存fromto和其他属性的值的信息,所以我也需要它们)和(3)byte[] EmailMessage.MimeContent.Content Actually I am lacking understanding of 其实我对此缺乏了解

  • Microsoft.Exchange.WebServices.Data.ItemView , Microsoft.Exchange.WebServices.Data.ItemView
  • Microsoft.Exchange.WebServices.Data.BasePropertySet and Microsoft.Exchange.WebServices.Data.BasePropertySet
  • Microsoft.Exchange.WebServices.Data.ItemSchema

thats why I am finding it difficult. 这就是为什么我发现它很难。

My primary code is: 我的主要代码是:

When I create PropertySet as follows: 当我按如下方式创建PropertySet

PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent);

I get following exception: 我得到以下异常:

The property MimeContent can't be used in FindItem requests.

I dont understand 我不明白

(1) What these ItemSchema and BasePropertySet are (1)这些ItemSchemaBasePropertySet是什么

(2) And how we are supposed to use them (2)我们应该如何使用它们

So I removed ItemSchema.MimeContent : 所以我删除了ItemSchema.MimeContent

PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties);

I wrote simple following code to get all mails in inbox: 我编写了简单的以下代码来获取收件箱中的所有邮件:

ItemView view = new ItemView(50);
view.PropertySet = properties;
FindItemsResults<Item> findResults; 
List<EmailMessage> emails = new List<EmailMessage>();

do
{    
    findResults = service.FindItems(WellKnownFolderName.Inbox, view);
    foreach (var item in findResults.Items)
    {
        emails.Add((EmailMessage)item);
    }
    Console.WriteLine("Loop");
    view.Offset = 50;
}
while (findResults.MoreAvailable);

Above I kept page size of ItemView to 50, to retrieve no more than 50 mails at a time, and then offsetting it by 50 to get next 50 mails if there are any. 在上面我将ItemView页面大小保持为50,一次检索不超过50封邮件,然后将其抵消50以获得下一封50封邮件(如果有的话)。 However it goes in infinite loop and continuously prints Loop on console. 然而,它进入无限循环并在控制台上连续打印Loop So I must be understanding pagesize and offset wrong. 所以我必须理解pagesizeoffset错误。 I want to understand 我想明白

(3) what pagesize , offset and offsetbasepoint in ItemView constructor means (3) ItemView构造函数中的pagesizeoffsetoffsetbasepoint意味着什么

(4) how they behave and (4)他们的表现如何

(5) how to use them to retrieve all mails in the inbox (5)如何使用它们来检索收件箱中的所有邮件

I didnt found any article online nicely explaining these but just giving code samples. 我没有在网上发现任何文章,很好地解释这些,但只是提供代码示例。 Will appreciate question-wise explanation despite it may turn long. 尽管可能需要很长时间,但仍会欣赏有问题的解释。

EWS is a bit inconsistent with the properties returned from various operations. EWS与各种操作返回的属性有点不一致。 An Item.Bind will not return the exact same properties as a FindItem. Item.Bind不会返回与FindItem完全相同的属性。 You're using PropertySets properly as far as defining what you want from the server, but you have to use them in the right place. 您正在使用PropertySets,只要在服务器上定义您想要的内容,但您必须在正确的位置使用它们。 What you need to do is find the items, then load the properties into them. 您需要做的是找到项目,然后将属性加载到它们中。 It's not ideal, but that's the way EWS works. 这不是理想的,但这就是EWS的工作方式。 With your loop, you're constantly assigning 50 to your offset when you need to increment it by 50. Off the top of my head, something like this would do: 使用循环时,当你需要将它增加50时,你会不断地为你的偏移量分配50.在我的头顶上,这样的事情会:

int offset = 0;
int pageSize = 50;
bool more = true;
ItemView view = new ItemView(pageSize, offset, OffsetBasePoint.Beginning);

view.PropertySet = PropertySet.IdOnly;
FindItemsResults<Item> findResults;
List<EmailMessage> emails = new List<EmailMessage>();

while(more){
    findResults = service.FindItems(WellKnownFolderName.Inbox, view);
    foreach (var item in findResults.Items){
        emails.Add((EmailMessage)item);
    }
    more = findResults.MoreAvailable;
    if (more){
        view.Offset += pageSize;
    }
}
PropertySet properties = (BasePropertySet.FirstClassProperties); //A PropertySet with the explicit properties you want goes here
service.LoadPropertiesForItems(emails, properties);

Now you have all of the items with all of the properties that you requested. 现在,您拥有了所有具有所请求属性的项目。 FindItems often doesn't return all of the properties you want even if you ask for them, so loading only the Id initially and then loading up the properties you want is generally the way to go. FindItems通常不会返回您想要的所有属性,因此最初只加载Id然后加载您想要的属性通常是要走的路。 You may also want to batch the loading of properties in some way depending on how many emails you're retrieving, perhaps in the loop prior to adding them to the List of EmailMessages. 您可能还希望以某种方式批量加载属性,具体取决于您检索的电子邮件数量,可能在将其添加到EmailMessages列表之前的循环中。 You could also consider other methods of getting items, such as a service.SyncFolder action. 您还可以考虑其他获取项的方法,例如service.SyncFolder操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Exchange Web服务托管API列出这些文件夹中的所有文件夹和文件? - How to list all folders and files in those folders using Exchange Web Services Managed API? Folder.Bind - “Id格式错误” - Exchange Web服务托管API - Folder.Bind - “Id is malformed” - Exchange Web Services Managed API Exchange Web服务托管API:访问其他用户项 - Exchange Web Services Managed API: Accessing other users items EWS托管的Api-从Office365交换服务器中检索电子邮件 - EWS Managed Api - Retrieving e-mails from Office365 exchange server 如何在托管的Exchange Web服务中更改AppointmentStatus - How to change AppointmentStatus in managed Exchange Web Services 如何使用Exchange Web服务从Exchange Server 2007获取所有未读邮件? - How to Get all unread mails from Exchange server 2007 using Exchange web service? C# 交换 Web 服务托管 API 模拟 -&gt; Microsoft Graph ZDB974238714CA8DE634A7CE108A14F - C# Exchange Web Services Managed API Impersonation -> Microsoft Graph API 如何使用 C# 和 Exchange Web Services Managed API 获取文件夹的策略? - How to get a folder's policy using C# and Exchange Web Services Managed API? Exchange Web服务托管API:如何通过FindItem方法执行重复扩展? - Exchange Web Services Managed API: How can I perform Recurrence Expansion through the FindItem Method? 如何使用Exchange Web Services 2010托管API获取文件夹大小? - How do I get folder size with Exchange Web Services 2010 Managed API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM