How to get the email body in HTML and TEXT from Exchange using EWS in C#?

18,433

Take a look at this thread on the Exchange Server Development Forum, I think it will answer your question. http://social.technet.microsoft.com/Forums/exchange/en-US/3c95b323-1ba2-4bc5-80bd-f5626707db6a/i-need-the-htmltext-and-the-plaintext-of-the-body-of-an-itemtype?forum=exchangesvrdevelopment


Update

I played around with this so I could provide a code sample, and it turns out you don't have to use extended properties. By default, EWS returns the HTML formatted body in the EmailMessageSchema.Body - so if you create a property set to include both ItemSchema.TextBody and EmailMessageSchema.Body, you can get both types in one Bind call.

public static void GetEmail(ExchangeService service, ItemId ItemId)
{
    PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.TextBody, EmailMessageSchema.Body);
    EmailMessage message = EmailMessage.Bind(service, ItemId, propSet);
}

This results in the following XML request:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
  <t:RequestServerVersion Version="Exchange2013" />
</soap:Header>
<soap:Body>
  <m:GetItem>
    <m:ItemShape>
      <t:BaseShape>IdOnly</t:BaseShape>
      <t:AdditionalProperties>
        <t:FieldURI FieldURI="item:TextBody" />
        <t:FieldURI FieldURI="item:Body" />
      </t:AdditionalProperties>
    </m:ItemShape>
    <m:ItemIds>
      <t:ItemId Id="AAMkADE4..." />
    </m:ItemIds>
  </m:GetItem>
</soap:Body>

And the following response:

<?xml version="1.0" encoding="utf-8"?>
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
      <h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="878" MinorBuildNumber="11" Version="V2_10" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <m:GetItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
    <m:ResponseMessages>
      <m:GetItemResponseMessage ResponseClass="Success">
        <m:ResponseCode>NoError</m:ResponseCode>
        <m:Items>
          <t:Message>
            <t:ItemId Id="AAMkADE4..." ChangeKey="CQAAABYAAAApjGm7TnMWQ5TzjbhziLL0AAGTja3C" />
            <t:Body BodyType="HTML" IsTruncated="false">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD  
                HTML 4.0 Transitional//EN"&gt;
                &lt;html&gt;
                &lt;head&gt;
                &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
               (Removed the rest of my HTML body)
            </t:Body>
            <t:TextBody BodyType="Text" IsTruncated="false">
             (Removed my text body)
            </t:TextBody>
          </t:Message>
        </m:Items>
      </m:GetItemResponseMessage>
    </m:ResponseMessages>
  </m:GetItemResponse>
</s:Body>

Hope that helps! Mimi

Share:
18,433

Related videos on Youtube

JuValencia
Author by

JuValencia

Just another developer freak :-)

Updated on September 16, 2022

Comments

  • JuValencia
    JuValencia over 1 year

    I have an application that reads the email from exchange using EWS. My problem is that to get the HTML version of the email is one call and to get the TEXT version of the email is another call.

    Is there a way without third party controls to get both formats in one call?

    Would be great to have some sample code.

  • JuValencia
    JuValencia about 10 years
    Thanks for your reply but I have read that post before it mentions the extender property but still no clear enough how to read it from that extended property and that is my problem.
  • JuValencia
    JuValencia about 9 years
    Hi Mimi pardon my ignorance so that means in C# code to get it will be something like: <br /> string txtBody = message.TextBody; string htmlBody = message.Body <br/> Like that?
  • JuValencia
    JuValencia about 9 years
    Another Question Mimi that option is only available for Exchange 2013 what about Exchange 2010 SP 2 how can we do the same?
  • irag10
    irag10 over 7 years
    @JulValencia - I believe in Exchange pre-2013 you either need to use the messy-looking extended properties or you need to make two calls to exchange, setting the RequestedBodyType to different types.
  • Kevin Swann
    Kevin Swann about 3 years
    If the email body is in HTML format, the ItemSchema.TextBody option that gives the BodyType="Text" elements in the returned XML is only supported in Exchange 2013 and above. If you try this in Exchange 2010, you will get a ServiceResponseException.