2014-04-09 65 views
1

我正在使用C#Exchaneg Web服务检索所有从邮箱的邮件Exchange 2010上如何获得邮件项目的项目ID在Exchange 2010中

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

我还需要每个电子邮件的唯一项目ID,以便在完成后,我可以将该电子邮件标记为在交换框上阅读。

我已经试过这样:

// 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); @"[email protected]" 

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

回答

2

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

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"; 

试试看。取决于你想如何做,我的语法可能会变得很糟糕。外汇大师格伦尺度上,有几个链接对AQS有点散MS文档一篇好的博客贴子:

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

+0

感谢,你知道我怎么能在SearchFilter得到添加发件人的电子邮件地址 - 我想做这样的事情: 'searchFilterCollection.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.From.Address,“@ domain.co.uk”));' ... 但它不会编译。 –

+1

没有EmailMessageSchema.From.Address。剪掉地址,我相信它应该可以工作。请注意,发件人和发件人不一定是同一件事。另外,请查看AQS查询,它可以搜索索引属性并为您提供更快的结果。尽管如此,它可能并不能涵盖你所搜寻的所有内容。 http://msdn.microsoft.com/en-us/library/office/dn579420%28v=exchg.150%29.aspx – user1017413

+0

我只用'EmailMessageSchema.From'尝试过了,它不匹配任何电子邮件在收件箱中,尽管他们在那里。 –