如何在Exchange 2010中获取邮件项的项ID

我在c#中使用Exchaneg Web Services来检索Exchange 2010上邮箱的所有电子邮件。

我将每封电子邮件的所有信息都放在一个返回给调用函数的数据表中。

我还需要每封电子邮件的唯一商品ID,以便在完成后我可以将电子邮件标记为“在Exchange上读取”框。

我试过这个:

// 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,所以我可以存储它,并在以后找到相同的项目,以便标记它读取

这是主要的代码块:

 //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 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?

Item.Bind返回一个Item,而不是ItemId,因此您得到一个编译时错误。 你已经拥有了ItemId。 除非您在Exchange中引用其他ID,例如EntryId,否则它就是您在每次迭代时获得的EmailMessage。

 ItemId itemID = email.Id; 

但是,如果您想要更新该代码块或其附近的项目,则不需要这样做。 为此,您只需进行更改(将它们标记为已读),然后将它们放入List或其他IEnumerable中,并使用ExchangeService.UpdateItems更新EmailMessages。

如果要存储ItemIds供以后使用,您应该知道ItemId不是永久的,不变的属性。 如果有问题的项目被移动到另一个邮箱或移动到另一个文件夹,它将会更改。 可能有其他情况可以改变它,例如服务包安装/版本升级,甚至是足够的时间,尽管我自己无法确认。

编辑:要回答以下声明,似乎ContainsSubstring在电子邮件地址上不能很好地工作。 您可以使用queryString执行此操作:

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

试一试。 根据你想要的方式,我的语法可能会脱落。 Exchange Guru Glen Scales有一篇不错的博客文章,其中有几个指向AQS上有点分散的MS文档的链接:

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