如何使用 Outlook Js 加载项在我的云服务器上上传 outlook 电子邮件附件
How to upload outlook email attachments on my cloud server using Outlook Js Add-in
我正在使用 vs2017 开发 outlook javascript 插件。我创建了一个示例应用程序来查找 outlook 邮件项目中的附件。在这里,从 Exchange Server 获取附件时,它 returns 200 OK。
我有自己的云应用程序,看起来像 google 驱动器。我想使用 POST API 调用在我的云服务器上上传 outlook 邮件附件。 API 调用 运行 成功。但是我无法从交换服务器获取文件内容。
我在这里添加了一些示例代码。
创建服务请求
/// <reference path="../App.js" />
var xhr;
var serviceRequest;
(function () {
"use strict";
// The Office initialize function must be run each time a new page is loaded
Office.initialize = function (reason) {
$(document).ready(function () {
app.initialize();
initApp();
});
};
function initApp() {
$("#footer").hide();
if (Office.context.mailbox.item.attachments == undefined) {
var testButton = document.getElementById("testButton");
testButton.onclick = "";
showToast("Not supported", "Attachments are not supported by your Exchange server.");
} else if (Office.context.mailbox.item.attachments.length == 0) {
var testButton = document.getElementById("testButton");
testButton.onclick = "";
showToast("No attachments", "There are no attachments on this item.");
} else {
// Initalize a context object for the app.
// Set the fields that are used on the request
// object to default values.
serviceRequest = new Object();
serviceRequest.attachmentToken = "";
serviceRequest.ewsUrl = Office.context.mailbox.ewsUrl;
serviceRequest.attachments = new Array();
}
};
})();
function testAttachments() {
Office.context.mailbox.getCallbackTokenAsync(attachmentTokenCallback);
};
function attachmentTokenCallback(asyncResult, userContext) {
if (asyncResult.status == "succeeded") {
serviceRequest.attachmentToken = asyncResult.value;
makeServiceRequest();
}
else {
showToast("Error", "Could not get callback token: " + asyncResult.error.message);
}
}
function makeServiceRequest() {
var attachment;
xhr = new XMLHttpRequest();
// Update the URL to point to your service location.
xhr.open("POST", "https://localhost:8080/GetOutlookAttachments/AttachmentExampleService/api/AttachmentService", true);
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.onreadystatechange = requestReadyStateChange;
// Translate the attachment details into a form easily understood by WCF.
for (i = 0; i < Office.context.mailbox.item.attachments.length; i++) {
attachment = Office.context.mailbox.item.attachments[i];
attachment = attachment._data$p[=10=] || attachment.[=10=]_0;
if (attachment !== undefined) {
serviceRequest.attachments[i] = JSON.parse(JSON.stringify(attachment));
}
}
// Send the request. The response is handled in the
// requestReadyStateChange function.
xhr.send(JSON.stringify(serviceRequest));
};
// Handles the response from the JSON web service.
function requestReadyStateChange() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
if (!response.isError) {
// The response indicates that the server recognized
// the client identity and processed the request.
// Show the response.
var names = "<h2>Attachments processed: " + response.attachmentsProcessed + "</h2>";
document.getElementById("names").innerHTML = names;
} else {
showToast("Runtime error", response.message);
}
} else {
if (xhr.status == 404) {
showToast("Service not found", "The app server could not be found.");
} else {
showToast("Unknown error", "There was an unexpected error: " + xhr.status + " -- " + xhr.statusText);
}
}
}
};
// Shows the service response.
function showResponse(response) {
showToast("Service Response", "Attachments processed: " + response.attachmentsProcessed);
}
// Displays a message for 10 seconds.
function showToast(title, message) {
var notice = document.getElementById("notice");
var output = document.getElementById('output');
notice.innerHTML = title;
output.innerHTML = message;
$("#footer").show("slow");
window.setTimeout(function () { $("#footer").hide("slow") }, 10000);
};
请帮我从outlook邮件中获取附件并上传到我的云服务器上。
attachment._data$p$0 提供附件元数据。从那里获取 id 并使用 getAttachmentContentAsync API 获取附件内容 Documentation
我正在使用 vs2017 开发 outlook javascript 插件。我创建了一个示例应用程序来查找 outlook 邮件项目中的附件。在这里,从 Exchange Server 获取附件时,它 returns 200 OK。
我有自己的云应用程序,看起来像 google 驱动器。我想使用 POST API 调用在我的云服务器上上传 outlook 邮件附件。 API 调用 运行 成功。但是我无法从交换服务器获取文件内容。
我在这里添加了一些示例代码。
创建服务请求
/// <reference path="../App.js" /> var xhr; var serviceRequest; (function () { "use strict"; // The Office initialize function must be run each time a new page is loaded Office.initialize = function (reason) { $(document).ready(function () { app.initialize(); initApp(); }); }; function initApp() { $("#footer").hide(); if (Office.context.mailbox.item.attachments == undefined) { var testButton = document.getElementById("testButton"); testButton.onclick = ""; showToast("Not supported", "Attachments are not supported by your Exchange server."); } else if (Office.context.mailbox.item.attachments.length == 0) { var testButton = document.getElementById("testButton"); testButton.onclick = ""; showToast("No attachments", "There are no attachments on this item."); } else { // Initalize a context object for the app. // Set the fields that are used on the request // object to default values. serviceRequest = new Object(); serviceRequest.attachmentToken = ""; serviceRequest.ewsUrl = Office.context.mailbox.ewsUrl; serviceRequest.attachments = new Array(); } }; })(); function testAttachments() { Office.context.mailbox.getCallbackTokenAsync(attachmentTokenCallback); }; function attachmentTokenCallback(asyncResult, userContext) { if (asyncResult.status == "succeeded") { serviceRequest.attachmentToken = asyncResult.value; makeServiceRequest(); } else { showToast("Error", "Could not get callback token: " + asyncResult.error.message); } } function makeServiceRequest() { var attachment; xhr = new XMLHttpRequest(); // Update the URL to point to your service location. xhr.open("POST", "https://localhost:8080/GetOutlookAttachments/AttachmentExampleService/api/AttachmentService", true); xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); xhr.onreadystatechange = requestReadyStateChange; // Translate the attachment details into a form easily understood by WCF. for (i = 0; i < Office.context.mailbox.item.attachments.length; i++) { attachment = Office.context.mailbox.item.attachments[i]; attachment = attachment._data$p[=10=] || attachment.[=10=]_0; if (attachment !== undefined) { serviceRequest.attachments[i] = JSON.parse(JSON.stringify(attachment)); } } // Send the request. The response is handled in the // requestReadyStateChange function. xhr.send(JSON.stringify(serviceRequest)); }; // Handles the response from the JSON web service. function requestReadyStateChange() { if (xhr.readyState == 4) { if (xhr.status == 200) { var response = JSON.parse(xhr.responseText); if (!response.isError) { // The response indicates that the server recognized // the client identity and processed the request. // Show the response. var names = "<h2>Attachments processed: " + response.attachmentsProcessed + "</h2>"; document.getElementById("names").innerHTML = names; } else { showToast("Runtime error", response.message); } } else { if (xhr.status == 404) { showToast("Service not found", "The app server could not be found."); } else { showToast("Unknown error", "There was an unexpected error: " + xhr.status + " -- " + xhr.statusText); } } } }; // Shows the service response. function showResponse(response) { showToast("Service Response", "Attachments processed: " + response.attachmentsProcessed); } // Displays a message for 10 seconds. function showToast(title, message) { var notice = document.getElementById("notice"); var output = document.getElementById('output'); notice.innerHTML = title; output.innerHTML = message; $("#footer").show("slow"); window.setTimeout(function () { $("#footer").hide("slow") }, 10000); };
请帮我从outlook邮件中获取附件并上传到我的云服务器上。
attachment._data$p$0 提供附件元数据。从那里获取 id 并使用 getAttachmentContentAsync API 获取附件内容 Documentation