-2

I want to fetch the email body text.Using below code am able to fetch the body in an unformatted HTML.Is there a way to fetch the body text directly?.Is there a way by which I can parse this unformatted HTML and fetch actual body.I want the text "Accesscode is 1603" as output.

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            service.AutodiscoverUrl("Deepak.kothari@domain.com");
            List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
            searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "AccessCode"));
            SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
            ItemView view = new ItemView(50);
            view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived);
            view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
            view.Traversal = ItemTraversal.Shallow;
            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
            foreach (Item myItem in findResults.Items)
            {
                myItem.Load();            
                if (myItem is EmailMessage)
                {

                    Console.WriteLine(HttpUtility.HtmlEncode((myItem as EmailMessage).Body));

                }

                else if (myItem is MeetingRequest)
                {
                    Console.WriteLine((myItem as MeetingRequest).Subject);
                }
                else
                {
                    // Else handle other item types.
                }

Note : Am using Exchange 2010.

enter image description here

Deepak Kothari
  • 1,601
  • 24
  • 31

1 Answers1

-1

Try the method ExchangeService.LoadPropertiesForItems.

Code:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
        service.AutodiscoverUrl("Deepak.kothari@domain.com");
        List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
        searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "AccessCode"));
        SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
        ItemView view = new ItemView(50);
        view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
        view.Traversal = ItemTraversal.Shallow;
        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
        service.LoadPropertiesForItems(findResults, view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived) { RequestedBodyType = BodyType.Text });
        foreach (Item myItem in findResults.Items)
        {
            myItem.Load();
            if (myItem is EmailMessage)
            {

                Console.WriteLine(HttpUtility.HtmlEncode((myItem as EmailMessage).Body));

            }

            else if (myItem is MeetingRequest)
            {
                Console.WriteLine((myItem as MeetingRequest).Subject);
            }
            else
            {
                // Else handle other item types.
            }

Update:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        service.AutodiscoverUrl("Deepak.kothari@domain.com");
        List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
        searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "AccessCode"));
        SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
        ItemView view = new ItemView(50);
        view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived){ RequestedBodyType = BodyType.Text };
        view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
        view.Traversal = ItemTraversal.Shallow;
        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
        foreach (Item myItem in findResults.Items)
        {
            myItem.Load();
            if (myItem is EmailMessage)
            {

                Console.WriteLine(HttpUtility.HtmlEncode((myItem as EmailMessage).Body));

            }

            else if (myItem is MeetingRequest)
            {
                Console.WriteLine((myItem as MeetingRequest).Subject);
            }
            else
            {
                // Else handle other item types.
            }
Community
  • 1
  • 1
NotTelling
  • 462
  • 3
  • 11