nmarus / node-ews

A simple JSON wrapper for the Exchange Web Services (EWS) SOAP API.
MIT License
116 stars 52 forks source link

has invalid child element 'Attachments' in namespace #122

Open jorgefo opened 4 years ago

jorgefo commented 4 years ago

Hi

I have this error when try to send email with attachments

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorSchemaValidation</faultcode>
         <faultstring xml:lang="es-CL">The request failed schema validation: The element \'Message\' in namespace \'http://schemas.microsoft.com/exchange/services/2006/types\' has invalid child element \'Attachments\' in namespace \'http://schemas.microsoft.com/exchange/services/2006/types\'.</faultstring>
         <detail>
            <e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">ErrorSchemaValidation</e:ResponseCode>
            <e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">The request failed schema validation.</e:Message>
            <t:MessageXml xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
               <t:LineNumber>1</t:LineNumber>
               <t:LinePosition>1283</t:LinePosition>
               <t:Violation>The element \'Message\' in namespace \'http://schemas.microsoft.com/exchange/services/2006/types\' has invalid child element \'Attachments\' in namespace \'http://schemas.microsoft.com/exchange/services/2006/types\'.</t:Violation>
            </t:MessageXml>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

xml send

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:tns="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:wsi="http://ws-i.org/schemas/conformanceClaim/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Header />
   <soap:Body>
      <CreateItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" MessageDisposition="SendAndSaveCopy">
         <SavedItemFolderId>
            <t:DistinguishedFolderId Id="sentitems" />
         </SavedItemFolderId>
         <Items>
            <t:Message>
               <t:ItemClass>IPM.Note</t:ItemClass>
               <t:Subject>Test EWS Email</t:Subject>
               <t:Body BodyType="HTML">This is a test email &lt;b&gt;demo&lt;/b&gt;</t:Body>
               <t:ToRecipients>
                  <t:Mailbox>
                     <t:EmailAddress>test@test.com</t:EmailAddress>
                  </t:Mailbox>
               </t:ToRecipients>
               <t:CcRecipients>
                  <t:Mailbox>
                     <t:EmailAddress>test_cc@test.com</t:EmailAddress>
                  </t:Mailbox>
               </t:CcRecipients>
               <t:IsRead>false</t:IsRead>
               <t:ReplyTo>
                  <t:Mailbox>
                     <t:EmailAddress>reply_to_test@test.com</t:EmailAddress>
                  </t:Mailbox>
               </t:ReplyTo>
               <t:Attachments>
                  <t:FileAttachment>
                     <t:Name>firstAttachment.txt</t:Name>
                     <t:ContentType>text/plain</t:ContentType>
                     <t:Content>VGhpcyBpcyB0aGUgc2Vjb25kIGZpbGUgYXR0YWNobWVudC4=</t:Content>
                  </t:FileAttachment>
               </t:Attachments>
            </t:Message>
         </Items>
      </CreateItem>
   </soap:Body>
</soap:Envelope>

and this is the code

const EWS = require('node-ews');
// exchange server connection info
const ewsConfig = {
  username: 'user',
  password: 'pass',
  host: 'https://ewsdomain.com'
};

// initialize node-ews
const ews = new EWS(ewsConfig);

// define ews api function
const ewsFunction = 'CreateItem';

// define ews api function args
const ewsArgs = {
  "attributes" : {
    "MessageDisposition" : "SendAndSaveCopy"
  },
  "SavedItemFolderId": {
    "DistinguishedFolderId": {
      "attributes": {
        "Id": "sentitems"
      }
    }
  },
  "Items" : {
    "Message" : {
      "ItemClass": "IPM.Note",
      "Subject" : "Test EWS Email",
      "Body" : {
        "attributes": {
          "BodyType" : "HTML"
        },
        "$value": "This is a test email <b>demo</b>"
      },
      "ToRecipients" : {
        "Mailbox" : {
          "EmailAddress" : "test@test.com"
        }
      },
      "CcRecipients" : {
        "Mailbox" : {
          "EmailAddress" : "test_cc@test.com"
        }
      },
      "IsRead": "false",
      "ReplyTo" : {
        "Mailbox" : {
          "EmailAddress" : "test_reply_to@test.com"
        }
      },
      "Attachments" : {
        "FileAttachment" : [{
          "Name" : "firstAttachment.txt",
          "ContentType" : "text/plain",
          "Content" : "VGhpcyBpcyB0aGUgc2Vjb25kIGZpbGUgYXR0YWNobWVudC4=",
        }]
      },
    }
  }
};

// query ews, print resulting JSON to console
ews.run(ewsFunction, ewsArgs)
  .then(result => {
    console.log(JSON.stringify(result));
  })
  .catch(err => {
    console.log(err);
  });
nmarus commented 4 years ago

Attachments is an array. https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/attachments-ex15websvcsotherref

jorgefo commented 4 years ago

Thanks for the answer

but i change this

"Attachments" : {
        "FileAttachment" : [{
          "Name" : "firstAttachment.txt",
          "ContentType" : "text/plain",
          "Content" : "VGhpcyBpcyB0aGUgc2Vjb25kIGZpbGUgYXR0YWNobWVudC4=",
        }]
      },

with this but the problem is the same


      "Attachments" : [{
        "FileAttachment" : [{
          "Name" : "firstAttachment.txt",
          "ContentType" : "text/plain",
          "Content" : "VGhpcyBpcyB0aGUgc2Vjb25kIGZpbGUgYXR0YWNobWVudC4=",
        }]
      }],