从Exchange Web Services Managed API中获取Inbox中的所有邮件,并将它们存储为.eml文件 [英] Fetching all mails in Inbox from Exchange Web Services Managed API and storing them as a .eml files

查看:150
本文介绍了从Exchange Web Services Managed API中获取Inbox中的所有邮件,并将它们存储为.eml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用EWS Managed API获取收件箱文件夹中的所有邮件,并将其存储为 .eml 。问题在于提取(1)(2)所有标题(如从,到,主题)的所有邮件(我保留这些值的$ $ c的信息$ c>从,和别的其他属性,所以我也需要它们)和 (3 )byte [] EmailMessage.MimeContent.Content 。实际上我对




  • Microsoft.Exchange.WebServices.Data.ItemView

  • Microsoft.Exchange.WebServices.Data.BasePropertySet

  • Microsoft.Exchange.WebServices.Data.ItemSchema



这就是为什么我发现很难。 / p>

我的主要代码是:



当我创建 PropertySet 如下:

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

我收到以下异常:

 属性MimeContent不能在FindItem请求中使用。 

我不明白



(1)这些 ItemSchema BasePropertySet



(2)我们应该如何使用它们



所以我删除了 ItemSchema.MimeContent

  PropertySet properties = new PropertySet(BasePropertySet.FirstClassProperties); 

我写了简单的以下代码来获取收件箱中的所有邮件:

  ItemView view = new ItemView(50); 
view.PropertySet = properties;
FindItemsResults< Item> findResults;
列表< EmailMessage>电子邮件=新列表< EmailMessage>();

do
{
findResults = service.FindItems(WellKnownFolderName.Inbox,view);
foreach(在findResults.Items中的var项目)
{
emails.Add((EmailMessage)项目);
}
Console.WriteLine(Loop);
view.Offset = 50;
}
while(findResults.MoreAvailable);

以上我将页面大小保持为 ItemView 50,一次检索不超过50封邮件,然后将其抵消50,以获得接下来的50封邮件(如果有的话)。然而,它进入无限循环,并在控制台上持续打印 Loop 。所以我必须了解 pagesize offset 错误。我想了解



(3)什么 pagesize offsetBasepoint ItemView 构造函数意味着



(4)他们的行为方式和



(5)检索收件箱中的所有邮件



我没有发现任何文章在线很好地解释这些,只是提供代码示例。尽管可能会变得长时间,但会欣赏问题解释。

解决方案

EWS与各种操作返回的属性有点不一致。 Item.Bind不会返回与FindItem完全相同的属性。您正在使用PropertySet来定义您想要的服务器,但必须在正确的位置使用它们。您需要做的是找到项目,然后将属性加载到它们中。这不是理想的,但这是EWS的工作方式。随着你的循环,当你需要增加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;
列表< EmailMessage>电子邮件=新列表< 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); //具有你想要的显式属性的PropertySet在这里
service.LoadPropertiesForItems(电子邮件,属性);

现在,您拥有您请求的所有属性的所有项目。 FindItems通常不会返回您想要的所有属性,即使您要求它们,因此最初只加载Id,然后加载所需的属性通常是要走的路。您可能还需要以某种方式批量加载属性,具体取决于您要检索的电子邮件数量,也许在将其添加到EmailMessages列表之前的循环中。您还可以考虑获取项目的其他方法,例如service.SyncFolder操作。


I want to fetch all mails in the Inbox folder using EWS Managed API and store them as .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. Actually I am lacking understanding of

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

thats why I am finding it difficult.

My primary code is:

When I create PropertySet as follows:

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

(2) And how we are supposed to use them

So I removed 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. However it goes in infinite loop and continuously prints Loop on console. So I must be understanding pagesize and offset wrong. I want to understand

(3) what pagesize, offset and offsetbasepoint in ItemView constructor means

(4) how they behave and

(5) how to use them to retrieve all mails in the inbox

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. An Item.Bind will not return the exact same properties as a 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. 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. 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:

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. 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. You could also consider other methods of getting items, such as a service.SyncFolder action.

这篇关于从Exchange Web Services Managed API中获取Inbox中的所有邮件,并将它们存储为.eml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆