【问题标题】:How to get the Item ID of a mail item in Exchange 2010如何在 Exchange 2010 中获取邮件项目的项目 ID
【发布时间】:2014-04-09 10:47:06
【问题描述】:

我在 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<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?

【问题讨论】:

    标签: c# email exchangewebservices exchange-server-2010


    【解决方案1】:

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

    ItemId itemID = email.Id;
    

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

    如果您想存储 ItemId 以供以后使用,您应该知道 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

    【讨论】:

    • 谢谢,你知道如何在 SearchFilter 中添加发件人的电子邮件地址吗?我想做这样的事情:searchFilterCollection.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.From.Address, "@domain.co.uk")); ...但它不会编译。
    • 没有 EmailMessageSchema.From.Address。剪掉 .Address ,我相信它应该可以工作。请注意,From 和 Sender 不一定是同一事物。此外,查看 AQS 查询,它可以搜索索引属性并为您提供更快的结果。但是,它可能无法涵盖您要搜索的所有内容,因此请尝试一下。 msdn.microsoft.com/en-us/library/office/…
    • 我只使用EmailMessageSchema.From 进行了尝试,但它与收件箱中的任何电子邮件都不匹配,尽管它们在那里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多