简体   繁体   中英

Fetch only text from email html body

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.

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);

"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM