Closed aaroncalderon closed 8 years ago
So, I figured it out. Turns out that when I perform a <GetItem />
on an email I created the body is returned as the '$value'
not as the _
(underscoer); see example:
{
"ResponseMessages": {
"GetItemResponseMessage": {
"attributes": {
"ResponseClass": "Success"
},
"ResponseCode": "NoError",
"Items": {
"Message": {
"MimeContent": {
"attributes": {
"CharacterSet": "UTF-8"
},
"$value": "VG86ICJDQUx..."
},
"ItemId": {
"attributes": {
"Id": "AAAVAEFh...",
"ChangeKey": "CQAAA..."
}
},
"Subject": "EWS node email [draft]",
"Sensitivity": "Normal",
"Body": {
"attributes": {
"BodyType": "HTML"
},
"$value": "<p><s>This</s> <u>is</u> <i>the</i> <b>Body.</b></p><h1>This is important.</h1>"
},
"Size": "26267",
"DateTimeSent": "2016-10-19T13:53:32Z",
"DateTimeCreated": "2016-10-19T13:53:32Z",
"ResponseObjects": {
"ForwardItem": null
},
"HasAttachments": "false",
"ToRecipients": {
"Mailbox": {
"Name": "SOME, ONE",
"EmailAddress": "SOMEONE@example.com",
"RoutingType": "SMTP"
}
},
"IsReadReceiptRequested": "false",
"IsDeliveryReceiptRequested": "false",
"IsRead": "true"
}
}
}
}
}
So, I didged around and found that xml2js
has an option to set the charkey
which defaults to _
.
So, if we change the file example I put earlyer like this:
var xml2js = require('xml2js');
var when = require('when');
var _ = require('lodash');
var util = require('util');
function convert(xml) {
var attrkey = 'attributes';
var charkey = '$value'; // added this to get the correct key > value that exchange expects
var parser = new xml2js.Parser({
attrkey: attrkey,
charkey: charkey,
trim: true,
ignoreAttrs: false,
explicitRoot: false,
explicitCharkey: false,
explicitArray: false,
explicitChildren: false,
tagNameProcessors: [
function(tag) {
return tag.replace('t:', '').replace('m:',''); // and this to cleanup some extra tags,
// not specifically used in this example but it is needed
}
]
});
return when.promise((resolve, reject) => {
parser.parseString(xml, (err, result) => {
if(err) reject(err);
else {
var ewsFunction = _.keys(result['soap:Body'])[0];
var parsed = result['soap:Body'][ewsFunction];
parsed[attrkey] = _.omit(parsed[attrkey], ['xmlns', 'xmlns:t']);
if(_.isEmpty(parsed[attrkey])) parsed = _.omit(parsed, [attrkey]);
resolve(parsed);
}
});
});
}
var xml = '<?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">' +
'<soap:Body>' +
'<CreateItem MessageDisposition="SendAndSaveCopy" xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">' +
'<SavedItemFolderId>' +
'<t:DistinguishedFolderId Id="sentitems" />' +
'</SavedItemFolderId>' +
'<Items>' +
'<t:Message>' +
'<t:ItemClass>IPM.Note</t:ItemClass>' +
'<t:Subject>EWS node email</t:Subject>' +
'<t:Body BodyType="HTML"><![CDATA[This is <b>bold</b> - <b>Priority</b> - Update specification]]></t:Body>' +
'<t:ToRecipients>' +
'<t:Mailbox>' +
'<t:EmailAddress>me@example.com</t:EmailAddress>' +
'</t:Mailbox>' +
'</t:ToRecipients>' +
'<t:IsRead>false</t:IsRead>' +
'</t:Message>' +
'</Items>' +
'</CreateItem>' +
'</soap:Body>' +
'</soap:Envelope>';
convert(xml).then(json => {
console.log('ewsArgs = ' + util.inspect(json, false, null));
});
// console output ready for ewsArgs
// ewsArgs = { attributes: { MessageDisposition: 'SendAndSaveCopy' },
// SavedItemFolderId: { DistinguishedFolderId: { attributes: { Id: 'sentitems' } } },
// Items:
// { Message:
// { ItemClass: 'IPM.Note',
// Subject: 'EWS node email',
// Body:
// { '$value': 'This is <b>bold</b> - <b>Priority</b> - Update specification',
// attributes: { BodyType: 'Text' } },
// ToRecipients: { Mailbox: { EmailAddress: 'me@example.com' } },
// IsRead: 'false' } } }
Thanks. Added your updated script in the converter folder.
I followed your example:
However, I get the following error
But, if I comment out the
_
attribute, the email is sent. So, I do not know how to add the HTML body, or just text body at all.Any suggestions?
Further testing
I'm planning on doing a
<CreateItem MessageDisposition="SaveOnly"...
and then an<UpdateItem...
and report back.