如何获得邮件项目的项目ID在Exchange 2010 [英] How to get the Item ID of a mail item in Exchange 2010

查看:1137
本文介绍了如何获得邮件项目的项目ID在Exchange 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#Exchaneg Web服务来检索Exchange 2010中的所有从邮箱的电子邮件。

I am using Exchaneg Web Services in c# to retrieve all the emails from a mailbox on Exchange 2010.

我把所有的信息,每封电子邮件在数据返回给调用函数表。

I am putting all the information for each email in a data table that is returned to the calling function.

我还需要每封电子邮件的唯一项目ID,这样我完成后,我可以标记邮件为已读在Exchange 。盒

I also need the unique Item ID of each email so that after I am finished I can mark the email as Read on the Exchange box.

我已经试过这样:

    // As a best practice, limit the properties returned to only those that are required.
    PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);

    // Bind to the existing item by using the ItemId.
    // This method call results in a GetItem call to EWS.
    ItemId itemID = Item.Bind(service, itemId, propSet);



但它不会编译,我不明白什么是错的,我需要的项目ID,以便以后我可以储存它,找到相同的项目,以便将其标记阅读

but it won't compile, and I don't understand what is wrong, I need the Item ID so I can store it and find the same item later in order to Mark it Read

这里是代码的主要块:

//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(common.strInboxURL); // new Mailbox(targetEmailAddress); @"bbtest@bocuk.local"

//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);

//this will bind the mailbox you're looking for using your service instance
Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, fid);

SearchFilter.SearchFilterCollection searchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And);
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true));
searchFilterCollection.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Sender, "@bankofcyprus.co.uk"));

// add the exceptions
for (int iEx = 0; iEx < e2c.emailExceptions.Count; iEx++)
{
    searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, e2c.emailExceptions[iEx])));                
}

ItemView view = new ItemView(100);
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
//view.PropertySet = new PropertySet(
//    BasePropertySet.IdOnly,
//    ItemSchema.Subject,
//    ItemSchema.DateTimeReceived);

// Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilterCollection, view);

foreach (EmailMessage email in results)
// looping through all the emails
{
    emailSenderName = email.Sender.Name;
    sEmailSubject = email.Subject;
    emailAttachmentsCount = email.Attachments.Count;
    emailDisplayTo = email.DisplayTo;
    emailHasAttachments = email.HasAttachments;

    email.Load(new PropertySet(ItemSchema.Body) { RequestedBodyType = BodyType.Text });
    sEmailBody = email.Body;

    // As a best practice, limit the properties returned to only those that are required.
    PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);

    // Bind to the existing item by using the ItemId. 
    // This method call results in a GetItem call to EWS.
    ItemId itemID = Item.Bind(service, itemId, propSet);


    //email.get .Load(new PropertySet(ItemId): 
    email.Load(new PropertySet(ItemSchema.DateTimeReceived));

    DataRow row = emails.NewRow();

    row["displayname"] = emailDisplayTo;
    ... (cut for brevity!
    email.Load(new PropertySet(EmailMessageSchema.Attachments));

我怎样才能获得该项目的ID,请

how can I get the item ID please?

推荐答案

Item.Bind返回一个项目,而不是一个ITEMID,所以你得到一个编译时错误,你已经拥有了ITEMID。它就在那里的EmailMessage你使用,除非你指的是在Exchange其他ID,如ENTRYID每次迭代得到。

Item.Bind returns an Item, not an ItemId, so you get a compile-time error. You already have the ItemId. It's right there on the EmailMessage you're getting with each iteration unless you're referring to other ids in Exchange, such as the EntryId.

ItemId itemID = email.Id;

这如果要更新的代码块中的项目是不必要或年前后,虽然。对于这一点,你只需要进行更改(它们标记为已读),然后将它们放入一个列表或其他IEnumerable和使用ExchangeService.UpdateItems更新EmailMessages。

This is unnecessary if you want to update the items in that block of code or thereabouts, though. For that, you just need to make the changes (mark them as read), then place them into a List or other IEnumerable and use ExchangeService.UpdateItems to update the EmailMessages.

如果你想存储供以后使用ItemIds,你应该知道,ITEMID不是永久不变的属性。如果有问题的项目移动到另一个邮箱或移动到另一个文件夹它会改变。 。可能有其他情况下,可以改变它,比如Service Pack安装/版本升级或足够的时间连的推移,虽然我无法证实这些自己

If you want to store the ItemIds for later use, you should know that the ItemId is NOT a permanent, unchanging property. It will change if the Item in question is moved to another mailbox or moved to another folder. There may be other cases that can change it, such as service pack installations/version upgrades or even the passage of enough time, although I cannot confirm those myself.

编辑:要回答下面的语句中,似乎ContainsSubstring不会对电子邮件地址的工作也很好。您可以将queryString做到这一点:

To answer the below statement, it seems that ContainsSubstring doesn't work too well on email addresses. You can do this with a queryString:

String queryString = "from:domain.co.uk AND isread:false AND hasattachment:true";



给一个尝试。我的语法可能是一个头发掉这取决于你想如何做到这一点。外汇大师格伦秤有有几个链接对AQS有点散MS文档一个不错的博客文章:

Give that a try. My syntax could be a hair off depending on how you want to do it. Exchange Guru Glen Scales has a nice blog post that has several links to the somewhat scattered MS documentation on AQS:

http://gsexdev.blogspot.com/2010/08/using-exchange-search-and- AQS-与-ews.html

这篇关于如何获得邮件项目的项目ID在Exchange 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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