2
votes

Hello I am using the following code to get the custom header from EWS.

But unfortunately it's not returning the header. I looked into the outlook for the headers using Mapi tool, where I can see the headers.

Any suggestions please.

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

1 Answers

0
votes

Naresh,

The inbox.FindItems() call won't return the internet headers. You'll need to update message.Load() to use a property set that includes headerProperty.

With regards,