Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 486
JuValencia Avatar asked Jan 07 '14 22:01

JuValencia


1 Answers

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

like image 104
Mimi Gentz Avatar answered Oct 02 '22 22:10

Mimi Gentz