Outlook Javascript API 获取邮件的 MIME 内容
Outlook Javascript API get MIME content of email
我需要将电子邮件的内容获取为 EML(最好是 base64,因为我需要将其发送到第 3 方系统)。
我正在使用这样的解决方案,但它有 1MB 的限制。
var soapEnvelope = ... // initialize soap envelope
Office.context.mailbox.makeEwsRequestAsync(soapEnvelope, function(result){
var parser = new DOMParser();
var doc = parser.parseFromString(result.value, "text/xml");
var values = doc.getElementsByTagName("t:MimeContent");
var subject = doc.getElementsByTagName("t:Subject");
console.log(subject[0].textContent)
});
有效,但有 1 MB 的限制。
我已经设法构建了一个直接调用 EWS 的解决方案。
这是一个 JavaScript 解决方案(可能对其他人有用,因为我找不到这样的例子)。
Office.context.mailbox.getCallbackTokenAsync(function(result) {
var token = result.value;
var ewsurl = Office.context.mailbox.ewsUrl;
var itemId = Office.context.mailbox.item.itemId;
var envelope = getSoapEnvelope(itemId);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", ewsurl, true);
xhttp.setRequestHeader("Content-type", "application/soap+xml");
xhttp.setRequestHeader("Authorization", "Bearer " + token);
xhttp.send(envelope);
xhttp.onload = function() {
// never comes here
};
xhttp.onprogress = function(event) {
// never comes here
};
xhttp.onerror = function() {
// COMES HERE IMMEDIATELY and ERROR ABOUT CORS IN CONSOLE
};
});
和XML SOAP 请求如下所示
function getSoapEnvelope(itemId) {
// Wrap an Exchange Web Services request in a SOAP envelope.
var result =
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' 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>' +
' <GetItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">' +
' <ItemShape>' +
' <t:BaseShape>IdOnly</t:BaseShape>' +
' <t:IncludeMimeContent>true</t:IncludeMimeContent>' +
' <AdditionalProperties xmlns="http://schemas.microsoft.com/exchange/services/2006/types">' +
' <FieldURI FieldURI="item:Subject" />' +
' </AdditionalProperties>' +
' </ItemShape>' +
' <ItemIds>' +
' <t:ItemId Id="' + itemId + '" />' +
' </ItemIds>' +
' </GetItem>' +
' </soap:Body>' +
'</soap:Envelope>';
return result;
}
如果我用 PostMan 模拟请求,否则 CORS 就可以工作。
Access to XMLHttpRequest at
'https://outlook.office365.com/EWS/Exchange.asmx' from origin 'https://myorg.github.io' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我有一个执行清单中请求的域
<AppDomains>
<AppDomain>https://myorg.github.io</AppDomain>
</AppDomains>
可能是什么问题?
我们使用 Exchange Online
谢谢。
我更新了原来的post。我设法构建了正确的 SOAP 请求,但遇到了另一个问题 (CORS)。
我无法解决 CORS 问题,而是听从了@outlookAdd-insTeam-MSFT 的建议,并移动了将电子邮件提取到我的服务器的逻辑(无论如何都必须存储电子邮件)。所以代码现在在服务器而不是前端(插件)上运行。
我需要将电子邮件的内容获取为 EML(最好是 base64,因为我需要将其发送到第 3 方系统)。
我正在使用这样的解决方案,但它有 1MB 的限制。
var soapEnvelope = ... // initialize soap envelope
Office.context.mailbox.makeEwsRequestAsync(soapEnvelope, function(result){
var parser = new DOMParser();
var doc = parser.parseFromString(result.value, "text/xml");
var values = doc.getElementsByTagName("t:MimeContent");
var subject = doc.getElementsByTagName("t:Subject");
console.log(subject[0].textContent)
});
有效,但有 1 MB 的限制。
我已经设法构建了一个直接调用 EWS 的解决方案。 这是一个 JavaScript 解决方案(可能对其他人有用,因为我找不到这样的例子)。
Office.context.mailbox.getCallbackTokenAsync(function(result) {
var token = result.value;
var ewsurl = Office.context.mailbox.ewsUrl;
var itemId = Office.context.mailbox.item.itemId;
var envelope = getSoapEnvelope(itemId);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", ewsurl, true);
xhttp.setRequestHeader("Content-type", "application/soap+xml");
xhttp.setRequestHeader("Authorization", "Bearer " + token);
xhttp.send(envelope);
xhttp.onload = function() {
// never comes here
};
xhttp.onprogress = function(event) {
// never comes here
};
xhttp.onerror = function() {
// COMES HERE IMMEDIATELY and ERROR ABOUT CORS IN CONSOLE
};
});
和XML SOAP 请求如下所示
function getSoapEnvelope(itemId) {
// Wrap an Exchange Web Services request in a SOAP envelope.
var result =
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' 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>' +
' <GetItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">' +
' <ItemShape>' +
' <t:BaseShape>IdOnly</t:BaseShape>' +
' <t:IncludeMimeContent>true</t:IncludeMimeContent>' +
' <AdditionalProperties xmlns="http://schemas.microsoft.com/exchange/services/2006/types">' +
' <FieldURI FieldURI="item:Subject" />' +
' </AdditionalProperties>' +
' </ItemShape>' +
' <ItemIds>' +
' <t:ItemId Id="' + itemId + '" />' +
' </ItemIds>' +
' </GetItem>' +
' </soap:Body>' +
'</soap:Envelope>';
return result;
}
如果我用 PostMan 模拟请求,否则 CORS 就可以工作。
Access to XMLHttpRequest at 'https://outlook.office365.com/EWS/Exchange.asmx' from origin 'https://myorg.github.io' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我有一个执行清单中请求的域
<AppDomains>
<AppDomain>https://myorg.github.io</AppDomain>
</AppDomains>
可能是什么问题?
我们使用 Exchange Online
谢谢。
我更新了原来的post。我设法构建了正确的 SOAP 请求,但遇到了另一个问题 (CORS)。
我无法解决 CORS 问题,而是听从了@outlookAdd-insTeam-MSFT 的建议,并移动了将电子邮件提取到我的服务器的逻辑(无论如何都必须存储电子邮件)。所以代码现在在服务器而不是前端(插件)上运行。