0

I'm using Exchange Service in c# to receive an email.

I'm using the following code:

var service = new ExchangeService
    {
        Credentials = new WebCredentials("somename", "somepass"),
        Url = new Uri("someurl")
    };
FindItemsResults <Item> findResults = service.FindItems(WellKnownFolderName.Inbox,new ItemView(1));
var item = findResults.Items[0];
item.Load();
return item.Body.Text;

It returns body in html. Is there any way i could get text only instead of html, i don't need html tags. Or should i parse it?

Thanks for any input.

midori
  • 4,807
  • 5
  • 34
  • 62
  • If there's no native solution, see http://stackoverflow.com/questions/19523913/remove-html-tags-from-string-including-nbsp-in-c-sharp – André Chalella Nov 13 '15 at 21:51
  • i was hoping for a Exchange solution, i thought i was missing some parameter. But anyway thanks – midori Nov 13 '15 at 21:55

2 Answers2

4

This worked for me.

var service = new ExchangeService
{
    Credentials = new WebCredentials("somename", "somepass"),
    Url = new Uri("someurl")
};

var itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties)
{
    RequestedBodyType = BodyType.Text
};

var itemview = new ItemView(1) {PropertySet = itempropertyset};
var findResults = service.FindItems(WellKnownFolderName.Inbox, itemview);
var item = findResults.FirstOrDefault();
item.Load(itempropertyset);
Console.WriteLine(item.Body);
Szabolcs Dézsi
  • 8,743
  • 21
  • 29
4

"In the PropertySet of your item you need to set the RequestedBodyType to BodyType.Text. Here's an example:"

PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);
itempropertyset.RequestedBodyType = BodyType.Text;
ItemView itemview = new ItemView(1000);
itemview.PropertySet = itempropertyset;

FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, "subject:TODO", itemview);
Item item = findResults.FirstOrDefault();
item.Load(itempropertyset);
Console.WriteLine(item.Body);

Citated from this answer.

Community
  • 1
  • 1
Jim Aho
  • 9,932
  • 15
  • 56
  • 87