Last active
January 9, 2019 17:12
-
-
Save JamesIgoe/05e53af69ef5eabfc3fa38d176f19925 to your computer and use it in GitHub Desktop.
Send Outlook email via EWS in Office API, written in JavaScript - MS code converted into reusable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mailForward = (function () { | |
"use strict"; | |
var mailForward = {}; | |
var _appName; | |
var _mailbox; | |
var _item_id; | |
var _changeKey; | |
var _attachmentName = "Email Attachment"; | |
var _attachmentMime; | |
var attachmentBody; | |
var attachmentSubject; | |
var attachmentTo; | |
var _subject; | |
var _adddresses; | |
var _adddressesSoap; | |
var _bodyText; | |
var _moveItem; | |
var _forwardAsAttachment; | |
var _targetFolder; | |
mailForward.initialize = function (appName) { | |
app.initialize(); | |
_appName = appName; | |
_mailbox = Office.context.mailbox; | |
var item = Office.context.mailbox.item; | |
_item_id = item.itemId; | |
mailForward.SaveDraftIfNecessary = function SaveDraftIfNecessary() { | |
if (_item_id == null || _item_id == undefined) { | |
Office.context.mailbox.item.saveAsync(function (asyncResult) { | |
if (asyncResult.error) { | |
app.showNotification(_appName, "The following error code was received: " + asyncResult.error.message); | |
} | |
else { | |
_item_id = asyncResult.value; | |
} | |
}); | |
} | |
}; | |
var RetrySendAsAttachment = function () { | |
var soapToGetItemId = | |
' <soap:Body>' + | |
' <GetItem' + | |
' xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"' + | |
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' + | |
' <ItemShape>' + | |
' <t:BaseShape>IdOnly</t:BaseShape>' + | |
' </ItemShape>' + | |
' <ItemIds>' + | |
' <t:ItemId Id="' + _item_id + '"/>' + | |
' </ItemIds>' + | |
' </GetItem>' + | |
' </soap:Body>'; | |
var soapToGetItemIdData = getSoapBoadyEnvelope(soapToGetItemId); | |
_mailbox.makeEwsRequestAsync(soapToGetItemIdData, retrySoapToGetItemIdDataCallback); | |
}; | |
var retrySoapToGetItemIdDataCallback = function retrySoapToGetItemIdDataCallback(asyncResult) { | |
if (asyncResult.error != null) { | |
app.showNotification(_appName, "soapToGetItemDataCallback: " + asyncResult.error.message); | |
} | |
else { | |
var response = asyncResult.value; | |
var xmlDoc = GetXmlDoc(response); | |
_changeKey = xmlDoc.getElementsByTagName("t:ItemId")[0].getAttribute("ChangeKey"); | |
forwardAttachmentAsNewEmail(_subject, "", _adddresses, true, _targetFolder); | |
} | |
}; | |
var forwardAttachmentAsNewEmail = function ForwardAttachmentAsNewEmail(subject, bodyText, targetAdddresses, moveItem, targetFolder) { | |
_forwardAsAttachment = true; | |
_subject = subject; | |
_bodyText = bodyText; | |
_adddresses = targetAdddresses; | |
setAddressesSoap(_adddresses); | |
_moveItem = moveItem; | |
_targetFolder = targetFolder; | |
var currentBody = item.body; | |
currentBody.getAsync(Office.CoercionType.Text, function (asyncResultBody) { | |
if (asyncResultBody.error) { | |
app.showNotification(_appName, "Error retreiving body: " + asyncResultBody.error.message); | |
} else { | |
attachmentBody = asyncResultBody.value; | |
//attachmentBody = attachmentBody.replace(/(?:\r\n|\r|\n)/g, '<br>'); | |
//attachmentBody = attachmentBody.replace(/\\\//g, "/"); | |
attachmentSubject = item.subject; | |
attachmentTo = returnAddresses(item.to); | |
var soapToCreateItem = | |
'<soap:Body>' + | |
' <m:CreateItem MessageDisposition="SendAndSaveCopy"' + | |
' xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" ' + | |
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' + | |
' <m:Items>' + | |
' <t:Message>' + | |
' <t:ItemClass>IPM.Note</t:ItemClass>' + | |
' <t:Subject>' + _subject + '</t:Subject>' + | |
' <t:Body BodyType="Text">' + _bodyText + '</t:Body>' + | |
' <t:ToRecipients>' + _adddressesSoap + '</t:ToRecipients>' + | |
' <t:Attachments>' + | |
' <t:ItemAttachment>' + | |
' <t:Name>' + _attachmentName + '</t:Name>' + | |
' <t:IsInline>false</t:IsInline>' + | |
' <t:Message>' + | |
' <t:Subject>' + attachmentSubject + '</t:Subject>' + | |
' <t:Body BodyType="Text">' + attachmentBody + '</t:Body>' + | |
' <t:ToRecipients>' + attachmentTo + '</t:ToRecipients>' + | |
' </t:Message>' + | |
' </t:ItemAttachment>' + | |
' </t:Attachments>' + | |
' </t:Message>' + | |
' </m:Items>' + | |
' </m:CreateItem>' + | |
' </soap:Body>'; | |
var soapToCreateItemData = getSoapBoadyEnvelope(soapToCreateItem); | |
_mailbox.makeEwsRequestAsync(soapToCreateItemData, soapToCreateItemDataCallback); | |
} | |
}); | |
//currentBody.getTypeAsync(function (asyncResultBodyType) { | |
// if (asyncResultBodyType.error) { | |
// app.showNotification(_appName, "Error retreiving body type: " + asyncResultBodyType.error.message); | |
// } | |
// else { | |
// var bodyType = asyncResultBodyType.value; | |
// app.showNotification(_appName, "Body Type: " + bodyType); | |
// currentBody.getAsync(Office.CoercionType.Text, function (asyncResultBody) { | |
// if (asyncResultBody.error) { | |
// app.showNotification(_appName, "Error retreiving body: " + asyncResultBody.error.message); | |
// } else { | |
// attachmentBody = asyncResultBody.value; | |
// attachmentSubject = item.subject; | |
// attachmentTo = returnAddresses(item.to); | |
// var soapToCreateItem = | |
// '<soap:Body>' + | |
// ' <m:CreateItem MessageDisposition="SendAndSaveCopy"' + | |
// ' xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" ' + | |
// ' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' + | |
// ' <m:Items>' + | |
// ' <t:Message>' + | |
// ' <t:ItemClass>IPM.Note</t:ItemClass>' + | |
// ' <t:Subject>' + _subject + '</t:Subject>' + | |
// ' <t:Body BodyType="Text">' + _bodyText + '</t:Body>' + | |
// ' <t:ToRecipients>' + _adddressesSoap + '</t:ToRecipients>' + | |
// ' <t:Attachments>' + | |
// ' <t:ItemAttachment>' + | |
// ' <t:Name>' + _attachmentName + '</t:Name>' + | |
// ' <t:IsInline>false</t:IsInline>' + | |
// ' <t:Message>' + | |
// ' <t:Subject>' + attachmentSubject + '</t:Subject>' + | |
// ' <t:Body BodyType="Text">' + attachmentBody + '</t:Body>' + | |
// ' <t:ToRecipients>' + attachmentTo + '</t:ToRecipients>' + | |
// ' </t:Message>' + | |
// ' </t:ItemAttachment>' + | |
// ' </t:Attachments>' + | |
// ' </t:Message>' + | |
// ' </m:Items>' + | |
// ' </m:CreateItem>' + | |
// ' </soap:Body>'; | |
// var soapToCreateItemData = getSoapBoadyEnvelope(soapToCreateItem); | |
// _mailbox.makeEwsRequestAsync(soapToCreateItemData, soapToCreateItemDataCallback); | |
// } | |
// }); | |
// } | |
//}); | |
}; | |
var soapToCreateItemDataCallback = function soapToCreateItemDataCallback(asyncResult) { | |
if (asyncResult.error != null) { | |
app.showNotification(_appName, "soapToCreateItemCallback: " + asyncResult.error.message); | |
} | |
else { | |
if (_moveItem == true) { | |
moveMessage(); | |
} | |
else { | |
app.showNotification(_appName, "Sent"); | |
} | |
} | |
}; | |
mailForward.SendNow = function SendNow(subject, bodyText, targetAdddresses, forwardAsAttachment, moveItem, targetFolder) { | |
_adddresses = targetAdddresses; | |
_moveItem = moveItem; | |
_forwardAsAttachment = forwardAsAttachment; | |
_targetFolder = targetFolder; | |
_subject = subject; | |
_bodyText = bodyText; | |
this.SaveDraftIfNecessary(); | |
var soapToGetItemDataBody; | |
if (forwardAsAttachment == true) { | |
soapToGetItemDataBody = | |
' <soap:Body>' + | |
' <GetItem' + | |
' xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"' + | |
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' + | |
' <ItemShape>' + | |
' <t:BaseShape>IdOnly</t:BaseShape>' + | |
' <t:IncludeMimeContent>true</t:IncludeMimeContent>' + | |
' </ItemShape>' + | |
' <ItemIds>' + | |
' <t:ItemId Id="' + _item_id + '"/>' + | |
' </ItemIds>' + | |
' </GetItem>' + | |
' </soap:Body>'; | |
} | |
else { | |
soapToGetItemDataBody = | |
' <soap:Body>' + | |
' <GetItem' + | |
' xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"' + | |
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' + | |
' <ItemShape>' + | |
' <t:BaseShape>IdOnly</t:BaseShape>' + | |
' </ItemShape>' + | |
' <ItemIds>' + | |
' <t:ItemId Id="' + _item_id + '"/>' + | |
' </ItemIds>' + | |
' </GetItem>' + | |
' </soap:Body>'; | |
} | |
var soapToGetItemData = getSoapBoadyEnvelope(soapToGetItemDataBody); | |
_mailbox.makeEwsRequestAsync(soapToGetItemData, soapToGetItemDataCallback); | |
}; | |
var soapToGetItemDataCallback = function soapToGetItemDataCallback(asyncResult) { | |
if (asyncResult.error != null) { | |
if (asyncResult.error.message == "Response exceeds 1 MB size limit. Please modify your EWS request.") { | |
RetrySendAsAttachment(); | |
} else { | |
app.showNotification(_appName, "soapToGetItemDataCallback: " + asyncResult.error.message); | |
} | |
} | |
else { | |
var response = asyncResult.value; | |
var xmlDoc = GetXmlDoc(response); | |
_changeKey = xmlDoc.getElementsByTagName("t:ItemId")[0].getAttribute("ChangeKey"); | |
setAddressesSoap(_adddresses); | |
if (_forwardAsAttachment == true) { | |
var content = xmlDoc.childNodes[0].textContent; | |
_attachmentMime = content.replace("NoError", ""); | |
forwardAsAttachment(); | |
} | |
else { | |
forwardAsMessage(); | |
} | |
} | |
}; | |
var forwardAsMessage = function forwardAsMessage() { | |
var soapToForwardItemBody = | |
'<soap:Body>' + | |
' <m:CreateItem MessageDisposition="SendAndSaveCopy">' + | |
' <m:Items>' + | |
' <t:ForwardItem>' + | |
' <t:Subject>' + _subject + '</t:Subject>' + | |
' <t:ToRecipients>' + _adddressesSoap + '</t:ToRecipients>' + | |
' <t:ReferenceItemId Id="' + _item_id + '" ChangeKey="' + _changeKey + '" />' + | |
' <t:NewBodyContent BodyType="Text">' + _bodyText + '</t:NewBodyContent>' + | |
' </t:ForwardItem>' + | |
' </m:Items>' + | |
' </m:CreateItem>' + | |
' </soap:Body>'; | |
var soapToForwardItem = getSoapBoadyEnvelope(soapToForwardItemBody); | |
_mailbox.makeEwsRequestAsync(soapToForwardItem, soapToForwardItemCallback); | |
}; | |
var forwardAsAttachment = function forwardAsAttachment() { | |
var soapToForwardAsAttachmentItemBody = | |
' <soap:Body>' + | |
' <m:CreateItem MessageDisposition="SendAndSaveCopy">' + | |
' <m:Items>' + | |
' <t:Message>' + | |
' <t:Subject>' + _subject + '</t:Subject>' + | |
' <t:Body BodyType="Text">' + _bodyText + '</t:Body>' + | |
' <t:Attachments>' + | |
' <t:ItemAttachment>' + | |
' <t:Name>' + _attachmentName + '</t:Name>' + | |
' <t:IsInline>false</t:IsInline>' + | |
' <t:Message>' + | |
' <t:MimeContent CharacterSet="UTF-8">' + _attachmentMime + '</t:MimeContent>' + | |
' </t:Message>' + | |
' </t:ItemAttachment>' + | |
' </t:Attachments>' + | |
' <t:ToRecipients>' + _adddressesSoap + '</t:ToRecipients>' + | |
' </t:Message>' + | |
' </m:Items>' + | |
' </m:CreateItem>' + | |
' </soap:Body>'; | |
var soapToForwardAsAttachmentItem = getSoapBoadyEnvelope(soapToForwardAsAttachmentItemBody); | |
_mailbox.makeEwsRequestAsync(soapToForwardAsAttachmentItem, soapToForwardItemCallback); | |
}; | |
var soapToForwardItemCallback = function soapToForwardItemCallback(asyncResult) { | |
if (asyncResult.error != null) { | |
app.showNotification(_appName, "soapToGetItemDataCallback: " + asyncResult.error.message); | |
} | |
else { | |
var response = asyncResult.value; | |
var xmlDoc = GetXmlDoc(response); | |
var result = xmlDoc.getElementsByTagName("m:ResponseCode")[0].textContent; | |
if (result == "NoError") { | |
if (_moveItem) { | |
moveMessage(); | |
} | |
else { | |
app.showNotification(_appName, "Sent"); | |
} | |
} | |
else { | |
app.showNotification(_appName, "soapToForwardItemCallback: Failed with error code " + result); | |
} | |
} | |
}; | |
var moveMessage = function moveMessage() { | |
var soapToMoveItemDataBody = | |
' <soap:Body>' + | |
' <m:MoveItem>' + | |
' <m:ToFolderId>' + | |
' <t:DistinguishedFolderId Id="' + _targetFolder + '" />' + | |
' </m:ToFolderId>' + | |
' <m:ItemIds>' + | |
' <t:ItemId Id="' + _item_id + '" ChangeKey="' + _changeKey + '" />' + | |
' </m:ItemIds >' + | |
' </m:MoveItem >' + | |
' </soap:Body>'; | |
var soapToMoveItemData = getSoapBoadyEnvelope(soapToMoveItemDataBody); | |
_mailbox.makeEwsRequestAsync(soapToMoveItemData, soapToMoveItemCallback); | |
}; | |
var soapToMoveItemCallback = function soapToMoveItemCallback(asyncResult) { | |
if (asyncResult.error != null) { | |
app.showNotification(_appName, "soapToMoveItemCallback: " + asyncResult.error.message); | |
} | |
else { | |
var response = asyncResult.value; | |
var xmlDoc = GetXmlDoc(response); | |
var result = xmlDoc.getElementsByTagName("m:ResponseCode")[0].textContent; | |
if (result == "NoError") { | |
app.showNotification(_appName, "Moved to " + _targetFolder + " folder!"); | |
} | |
else { | |
app.showNotification(_appName, "The following error code was received: " + result); | |
} | |
} | |
}; | |
//converts response to XMLdoc | |
var GetXmlDoc = function GetXmlDoc(responseValue) { | |
var parser; | |
var xmlDoc; | |
if (window.DOMParser) { | |
parser = new DOMParser(); | |
xmlDoc = parser.parseFromString(responseValue, "text/xml"); | |
} | |
else // Older Versions of Internet Explorer | |
{ | |
xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); | |
xmlDoc.async = false; | |
xmlDoc.loadXML(responseValue); | |
} | |
return xmlDoc; | |
}; | |
//wraps body in standard soap | |
var getSoapBoadyEnvelope = function getSoapBoadyEnvelope(soapBody) { | |
var soapMessage = '<?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>' + soapBody + | |
'</soap:Envelope>'; | |
return soapMessage; | |
}; | |
//Sets _addressSoap in the appropriate wrapper for soap messages | |
var returnAddresses = function returnAddresses(recipients) { | |
var recipientsLength = recipients.length; | |
var addressesString = ""; | |
for (var recipient = 0; recipient < recipientsLength; recipient++) { | |
addressesString += "<t:Mailbox><t:EmailAddress>" + recipients[recipient].emailAddress + "</t:EmailAddress></t:Mailbox>"; | |
} | |
return addressesString; | |
}; | |
//Sets _addressSoap in the appropriate wrapper for soap messages | |
var setAddressesSoap = function setAddressesSoap(recipients) { | |
var toAddresses = recipients; | |
var addresses = toAddresses.split(";"); | |
_adddressesSoap = ""; | |
var addressesLength = addresses.length; | |
for (var address = 0; address < addressesLength; address++) { | |
_adddressesSoap += "<t:Mailbox><t:EmailAddress>" + addresses[address] + "</t:EmailAddress></t:Mailbox>"; | |
} | |
}; | |
}; | |
return mailForward; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment