如何使用Office JS API获取Outlook电子邮件的原始内容?

时间:2018-07-04 18:00:37

标签: outlook outlook-addin office-js

我正在尝试检查发件人是否使用了代理电子邮件地址。比较from和sender属性是不够的,因此我想到了检查原始消息源本身。

如何使用Office JS API获取原始消息源?

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您只是在寻找一种预构建的解决方案来查看和分析Outlook和OWA中的邮件头,则可以使用Message Header Analyzer。如果您要构建自己的加载项,则可以从那里借用源代码。

基本上,您有两个选择:

  1. EWS
  2. 休息

在两种情况下,您要检索的是PR_TRANSPORT_MESSAGE_HEADER,也就是0x007D。 EWS请求的外观类似于this

function getHeadersRequest(id) {
    // Return a GetItem EWS operation request for the headers of the specified item.
    return "<GetItem xmlns='http://schemas.microsoft.com/exchange/services/2006/messages'>" +
        "  <ItemShape>" +
        "    <t:BaseShape>IdOnly</t:BaseShape>" +
        "    <t:BodyType>Text</t:BodyType>" +
        "    <t:AdditionalProperties>" +
        // PR_TRANSPORT_MESSAGE_HEADERS
        "      <t:ExtendedFieldURI PropertyTag='0x007D' PropertyType='String' />" +
        "    </t:AdditionalProperties>" +
        "  </ItemShape>" +
        "  <ItemIds><t:ItemId Id='" + id + "'/></ItemIds>" +
        "</GetItem>";
}

您将通过致电makeEwsRequestAsync

提交
    var mailbox = Office.context.mailbox;
    var request = getHeadersRequest(mailbox.item.itemId);
    var envelope = getSoapEnvelope(request);
    mailbox.makeEwsRequestAsync(envelope, function (asyncResult) {
        callbackEws(asyncResult, headersLoadedCallback);
    });

要从休息开始做同样的事情,您首先需要获得商品的rest ID

function getItemRestId() {
if (Office.context.mailbox.diagnostics.hostName === "OutlookIOS") {
    // itemId is already REST-formatted
    return Office.context.mailbox.item.itemId;
} else {
    // Convert to an item ID for API v2.0
    return Office.context.mailbox.convertToRestId(
        Office.context.mailbox.item.itemId,
        Office.MailboxEnums.RestVersion.v2_0
    );
}

然后通过AJAX发送请求:

var getMessageUrl = getRestUrl(accessToken) +
    "/api/v2.0/me/messages/" +
    itemId +
    // PR_TRANSPORT_MESSAGE_HEADERS
    "?$select=SingleValueExtendedProperties&$expand=SingleValueExtendedProperties($filter=PropertyId eq 'String 0x007D')";

$.ajax({
    url: getMessageUrl,
    dataType: "json",
    headers: {
        "Authorization": "Bearer " + accessToken,
        "Accept": "application/json; odata.metadata=none"
    }
}).done(function (item) {

MHA源提供了更多上下文。