I have a VB.Net Visual Studio application. With Exchange Web Services I was able to the read an email and get its subject and the from. I am having troubles to find a way to read its body. I found four different ways of doing it but they all give me the same result, that is the text is like this:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
........ MORE HTML
Which it looks like it gives me the html of the email. How can I just get the text in the body of the email? What would be the code for that? This is the code I have with the four approaches I tried:
Dim iv As ItemView = New ItemView(999)
iv.Traversal = ItemTraversal.Shallow
Dim inboxItems As FindItemsResults(Of Item) = Nothing
inboxItems = exch.FindItems(WellKnownFolderName.Inbox, iv)
Dim Count As Integer = inboxItems.Count
For Each i As Item In inboxItems
Dim Subject = i.Subject
Dim oMail As EmailMessage = EmailMessage.Bind(exch, i.Id)
Dim email = oMail.From.Address.ToString
'FIRST WAY TRIED
Dim body = i.Body.Text
'SECOND WAY TRIED
i.Load()
Dim bodyagain = i.Body
'THIRD WAY TRIED
Dim item As Item = Item.Bind(exch, i.Id)
Dim bodynew = item.Body.ToString
'FOURTH WAY TRIED
For Each msg As EmailMessage In inboxItems
msg.Load()
Dim bodyTry = msg.Body
Next
Next
EWS will return the HTML body by default which is why your always getting that result. If you want the Text body instead you need to use Property set and specify that, then load that property when you use either Load or bind eg
Dim bodyPropSet As New PropertySet(BasePropertySet.FirstClassProperties)
bodyPropSet.Add(ItemSchema.Body)
bodyPropSet.RequestedBodyType.Value = BodyType.Text
i.Load(bodyPropSet)
Dim bodyText = i.Body.Text
If you doing this a lot then look at using LoadPropertiesForItems to make it more efficient as that code loop will perform pretty poorly with a large number of items.