29

I use EWS to get exchange emails, but how can i get plain text from email body, without html?
Now i use this:

EmailMessage item = (EmailMessage)outbox.Items[i];
item.Load();
item.Body.Text
JNM
  • 1,175
  • 4
  • 20
  • 39

5 Answers5

69

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);
Elijah W. Gagne
  • 2,801
  • 4
  • 31
  • 29
  • 7
    Note that the PropertySet has to be used for both service.FindItems() and item.Load() for this to work properly. – Dave Jan 15 '13 at 18:29
  • I am getting this exception on doing this Microsoft.Exchange.WebServices.Data.ServiceObjectPropertyException: You must load or assign this property before you can read its value – kolexinfos Mar 17 '16 at 18:48
  • @JNM just FYI. There is a more convenient way to work with Exchange servers than EWS, through Aurinko.io REST API. Example from the docs https://apirefs.aurinko.io/#operation/message – Alexey Oct 09 '20 at 15:18
7

In powershell:

    .........    
$message = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($event.MessageData,$itmId)

$PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text
$message.Load($PropertySet)
$bodyText= $message.Body.toString()
Liz Barraza
  • 133
  • 1
  • 7
  • What is `$event` in this case?? `$itmId`???? - I have an EmailMessage, and I'm using the exact same code as your last 4 lines, but it's still returning HTML instead of plaintext. I'll try to remember to come back if I figure it out... :-/ – Code Jockey Jun 06 '16 at 20:25
  • If anyone else is curious, just use your EWS service object in place of $event. – Preston Mar 13 '18 at 21:57
7

I had the same issue. All you have to do is set RequestedBodyType property of the property set you are using.

    PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.Subject, EmailMessageSchema.Body);
    propSet.RequestedBodyType = BodyType.Text;
    var email = EmailMessage.Bind(service, item.Id, propSet);
cookiemonster
  • 131
  • 1
  • 5
5

The shortest way to do it is like this:

item.Load(new PropertySet(BasePropertySet.IdOnly, ItemSchema.TextBody, EmailMessageSchema.Body));

This has got the advantage that you get both, text-body and html-body.

CaptainInler
  • 157
  • 1
  • 9
Rodger Svovah
  • 51
  • 1
  • 2
3

you can use

service.LoadPropertiesForItems(findResults, itempropertyset);

to load properties for all items

Xavi
  • 20,111
  • 14
  • 72
  • 63
Petr
  • 2,603
  • 2
  • 17
  • 9
  • or if you know item unique id: `PropertySet plainTextPropertySet = new PropertySet(BasePropertySet.FirstClassProperties) { RequestedBodyType = BodyType.Text, }; EmailMessage emailMessage = EmailMessage.Bind(service, uniqueId, plainTextPropertySet); string body = emailMessage.Body.Text;` – SamFlushing Oct 27 '14 at 14:54