使用 javascript 函数提取 outlook 的邮件 body

Extract mail body of outlook using javascript function

我正在尝试使用 javascript 获取我的 outlook 邮件的 body。目前我可以提取标题但不能 body.

document.getElementById("fill-data").onclick = function () {
        var item = Office.context.mailbox.item;
        var bodyContent;
        Office.context.mailbox.item.body.getAsync('text', function (async) {bodyContent = async.value});
        document.getElementById("txt-subject").value = item.subject;
       // The below code returns "undefined"
        document.getElementById("txt-description").value = bodyContent;  
    };

Office.context.mailbox.item.body.getAsync('text', 函数(异步){bodyContent = async.value});没有返回 body,而是 returns“未定义”到我的文本框中。我如何提取 outlook 邮件的 body?

Office.context.mailbox.item.body.getAsync(
    'text', 
    function (async) {bodyContent = async.value}
);

在其回调中设置 bodyContent 的值。回调被调用异步(即对Office.context.mailbox.item.body.getAsync的调用不会阻塞)。

这意味着当你

document.getElementById("txt-description").value = bodyContent;

那个回调函数还没有被调用,bodyContent还没有分配给

在调用回调之前不要设置 value。如何?只是在回调中这样做是一种方式。

Office.context.mailbox.item.body.getAsync(
    'text', 
    function (result) {
        document.getElementById("txt-description").value = result.value;
    }
);

旁注:我强烈建议不要使用变量名 async,即使您的环境允许。