使用 EWS 托管 api Nodejs 实现从自定义文件夹中读取 MS-Exchange 电子邮件

Read MS-Exchange emails from a custom folder using EWS managed api Nodejs implementation

有没有办法使用 EWS 托管 api(NodeJs 实现)从 MS-Exchange 中的自定义文件夹中读取电子邮件?我可以从收件箱中读取,但我有自定义文件夹名称,我希望在这些文件夹中读取代码。

我试过的。

const EWS = require('node-ews');
const ewsConfig = {
    username: '<Email>',
    password: '<Password>',
    host: '<Exchange URL>'
};
const ews = new EWS(ewsConfig);
const ewsFunction = 'FindItem';
var ewsArgs = {
    'attributes': {
        'Traversal': 'Shallow'
    },
    'ItemShape': {
        't:BaseShape': 'IdOnly',
        't:AdditionalProperties': {
            't:FieldURI': {
                'attributes': {
                    'FieldURI': 'item:Subject'
                }
            }
        }
    },
    'ParentFolderIds': {
        'DistinguishedFolderId': {
            'attributes': {
                'Id': '<Some Custom Folder>'
            }
        }
    }
};

(async function () {
   
    try {
        let result = await ews.run(ewsFunction, ewsArgs);
        console.log(result);
    } catch (err) {
        console.log(err.message);
    }
})();
    

错误:

a:ErrorInvalidRequest: The request is invalid.: {"ResponseCode":"ErrorInvalidRequest","Message":"The request is invalid."}

DistinguishedFolderId 不适用于非默认文件夹,所以我建议您试试

    'ParentFolderIds': {
        'FolderId': {
            'attributes': {
                'Id': '<Some Custom Folder>'
            }
        }
    }

我让它工作的方法是首先使用 FindFolder 调用找到 FolderId

const ewsArgs = {
  FolderShape: {
    BaseShape: 'AllProperties',
  },
  ParentFolderIds: {
    DistinguishedFolderId: {
      attributes: {
        Id: 'inbox',
      },
      Mailbox: {
        EmailAddress: 'emailaddress@company.com',
      },
    },
  },
};

const { ResponseMessages } = await ews.run('FindFolder', ewsArgs, ews.ewsSoapHeader);

const found = ResponseMessages.FindFolderResponseMessage.RootFolder.Folders.Folder
  .find(f => f.DisplayName.match(new RegExp(folderName.toLowerCase(), 'ig')));

之后,您可以使用它通过 FindItem 调用查找文件夹中的所有电子邮件:

const ewsArgs = {
  attributes: {
    Traversal: 'Shallow',
  },
  ItemShape: {
    BaseShape: 'IdOnly',
    // BaseShape: 'AllProperties',
  },
  ParentFolderIds: {
    FolderId: found.FolderId,
  },
};

const { ResponseMessages } = await ews.run('FindItem', ewsArgs, ews.ewsSoapHeader);