none
EWS: Cannot append HTML formatted text to HTML body when using CreateItem to forward an email RRS feed

  • Question

  • I want to append some text to the beginning of the body of an email I'm forwarding.  The text I'm inserting is complex and seems to generate various ErrorSchemaValidation errors. Here's what I've tried for the value of the NewBodyContent element (with BodyType="HTML"; the body text is passed via the headers parameter):

    • Used complex text (in this case the full Internet Message headers from another email) - fails, no text is appended
    • Used encodeURI on complex text - works, but all the encoded values are not transformed to HTML and the appended text appears as Plain Text (although source body is still HTML; e.g. the appended text reads as "From:%20username %20%3Cuser@domain.com%3E%0A" instead of "From: username <user@domain.com>")
    • Inserted 'Hello world!' - works
    • Inserted '<H1>Hello world!</H1>' - fails, no text is appended

    I'm baffled.  Any ideas on how to properly append complex text to the HTML body?

        function forwardItemRequest(item_id, email, changeKey, headers) {
            var request;
    
            // The following string is a valid SOAP envelope and request for forwarding
            // a mail item. Note that we use the item_id value to specify the item we are interested in,
            // along with its ChangeKey value that we have just determined
            // We also provide the XML fragment to specify the recipient addresses
    
            //TEST 5: Try with HTML content
            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:xsd="http://www.w3.org/2001/XMLSchema"' +
                '               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' +
                '               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
                '  <soap:Header>' +
                '    <RequestServerVersion Version="Exchange2013" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" soap:mustUnderstand="0" />' +
                '  </soap:Header>' +
                '  <soap:Body>' +
                '    <m:CreateItem MessageDisposition="SendAndSaveCopy">' +
                '      <m:Items>' +
                '        <t:ForwardItem>' +
                '          <t:ToRecipients>' + email + '</t:ToRecipients>' +
                '          <t:ReferenceItemId Id="' + item_id + '" ChangeKey="' + changeKey + '" />' +
                '          <t:NewBodyContent BodyType="HTML">' + headers + '</t:NewBodyContent>' +
                '        </t:ForwardItem>' +
                '      </m:Items>' +
                '    </m:CreateItem>' +
                '  </soap:Body>' +
                '</soap:Envelope>';
    
    
            return request;
        }


    Eric Legault (MVP: Outlook)
    Co-author, Microsoft Office 2013 Professional Step-By-Step

    Monday, October 26, 2015 4:49 PM

Answers

  • If your using encodeURI then your HTML won't be encoded correctly as EncodeURI is designed to encode URL's not HTML eg your want you POST to be encoded like

      <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_SP1" />
        </soap:Header>
        <soap:Body>
          <m:CreateItem MessageDisposition="SendOnly">
            <m:Items>
              <t:ForwardItem>
                <t:ToRecipients>
                  <t:Mailbox>
                    <t:EmailAddress>user@domain.com</t:EmailAddress>
                  </t:Mailbox>
                </t:ToRecipients>
                <t:ReferenceItemId Id="A=" ChangeKey="CQAAABYAAAB1EEf9x" />
                <t:NewBodyContent BodyType="HTML">&lt;H1&gt;Hello world!&lt;/H1&gt;</t:NewBodyContent>
              </t:ForwardItem>
            </m:Items>
          </m:CreateItem>
        </soap:Body>
      </soap:Envelope>

    In JavaScript the easiest way to do that is just use JQuery eg http://stackoverflow.com/questions/1219860/html-encoding-in-javascript-jquery and that should work fine eg https://jsfiddle.net/p4rua82k/

    Cheers
    Glen

     
    Tuesday, October 27, 2015 4:18 AM

All replies

  • If your using encodeURI then your HTML won't be encoded correctly as EncodeURI is designed to encode URL's not HTML eg your want you POST to be encoded like

      <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_SP1" />
        </soap:Header>
        <soap:Body>
          <m:CreateItem MessageDisposition="SendOnly">
            <m:Items>
              <t:ForwardItem>
                <t:ToRecipients>
                  <t:Mailbox>
                    <t:EmailAddress>user@domain.com</t:EmailAddress>
                  </t:Mailbox>
                </t:ToRecipients>
                <t:ReferenceItemId Id="A=" ChangeKey="CQAAABYAAAB1EEf9x" />
                <t:NewBodyContent BodyType="HTML">&lt;H1&gt;Hello world!&lt;/H1&gt;</t:NewBodyContent>
              </t:ForwardItem>
            </m:Items>
          </m:CreateItem>
        </soap:Body>
      </soap:Envelope>

    In JavaScript the easiest way to do that is just use JQuery eg http://stackoverflow.com/questions/1219860/html-encoding-in-javascript-jquery and that should work fine eg https://jsfiddle.net/p4rua82k/

    Cheers
    Glen

     
    Tuesday, October 27, 2015 4:18 AM
  • Shame on me for assuming what encodeURI does!  Your suggestion/link is perfect - thanks Glen!

    Eric Legault (MVP: Outlook)
    Co-author, Microsoft Office 2013 Professional Step-By-Step

    Tuesday, October 27, 2015 6:18 PM