EWS - 获取自定义标题

时间:2014-02-12 06:42:15

标签: c# outlook exchangewebservices outlook-2010

您好我使用以下代码从EWS获取自定义标头。

但不幸的是,它没有返回标题。我使用Mapi工具查看了标题的前景,在那里我可以看到标题。

请提出任何建议。

service = ExchangeServiceHelpers.GetBinding();

                // Bind the Inbox folder to the service object
                var inbox = Folder.Bind(service, WellKnownFolderName.Inbox);

                var searchFilter = ExchangeServiceHelpers.PopulateSearchFilters();

                var view = new ItemView(int.MaxValue); //  Search operation should return maximum number of elements.

                // Defines a property set that contains the schematized Internet message headers. 


                var headerProperty = new ExtendedPropertyDefinition(
                      DefaultExtendedPropertySet.InternetHeaders,
                     "x-worksitefolderemailid",
                      MapiPropertyType.String);

                var columns = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.InternetMessageId, headerProperty);


                view.PropertySet = columns;

                // Fire the query for the unread items
                var findResults = inbox.FindItems(searchFilter, view);



                // Loop through the search results.
                foreach (EmailMessage message in findResults)
                {
                    try
                    {
                        message.Load(
                            new PropertySet(new PropertyDefinitionBase[] { ItemSchema.MimeContent, ItemSchema.Subject}));

                        string mailAddress = GetFolderId(message, headerProperty); // Get internet header

                        if (string.IsNullOrEmpty(mailAddress))
                        {
                            Logger.Info(
                                string.Format("Email '{0}' doesn't have folder id address. Marking as Read Item.",
                                              message.Subject));
                            ExchangeServiceHelpers.MarkMessageAsRead(service, message.Id); // Marking the email item as Read prevents the item to be returned in further search results.
                            continue;
                        }



                    }
                    catch (Exception e)
                    {
                        Logger.Error(e);
                    }
                }

private static string GetFolderId(EmailMessage message, ExtendedPropertyDefinition headerProperty)
        {
            try
            {


                if (message.ExtendedProperties == null || message.ExtendedProperties.Count == 0)
                {
                    Logger.Info(
                        string.Format("Email '{0}' doesn't have any extended properties. Marking as Read Item.",
                                      message.Subject));
                    return string.Empty;
                }
                //message.InternetMessageHeaders
                foreach (ExtendedProperty property in message.ExtendedProperties)
                {
                    if (property.PropertyDefinition == headerProperty)
                    {
                        return property.Value.ToString();
                    }
                }

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

            return string.Empty;
        }

1 个答案:

答案 0 :(得分:0)

纳雷什,

inbox.FindItems()调用不会返回互联网标题。您需要更新message.Load()以使用包含headerProperty的属性集。

关于,