首页 > 解决方案 > 如何使用 node-ews 包阅读带有正文和附件的未读电子邮件

问题描述

我可以使用这个 node-ews 包发送电子邮件,但我找不到合适的示例来从收件箱文件夹中读取电子邮件并从电子邮件中获取正文和附件。

我浏览了 Microsoft 文档,例如https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-work-with-exchange-mailbox-items-by- using-ews-in-exchange#get-an-item-by-using-the-ews-managed-api 但示例以 C#、C++ 或 VB 提供。

但我想用 Nodejs 做到这一点。

标签: node.jsemailmicrosoft-exchange

解决方案


您可以使用以下代码使用 FindItem 函数从 Inbox 获取电子邮件,然后使用 GetItem 函数阅读每封电子邮件

 // Read emails from Inbox
    var ewsFunction = 'FindItem';
    var ewsArgs = {
        'attributes': {
            'Traversal': 'Shallow'
        },
        'ItemShape': {
            't:BaseShape': 'IdOnly',
            't:AdditionalProperties': {
            't:FieldURI': {
                'attributes': {
                'FieldURI': 'item:Subject'
                }
            }
          }
        },
        'ParentFolderIds' : {
            'DistinguishedFolderId': {
            'attributes': {
                'Id': 'inbox'
            }
          }
        }
    };
    // Itreate over all the emails and store Id and ChangeKey.
    ews.run(ewsFunction, ewsArgs, ewsSoapHeader)
    .then(result => {
        // Iterate over the result and extract Id and ChangeKey of the messages and pass those to GetItem function to read messages
    })
    
    // For reading individual messages returned by FindItem (using Id and ChangeKey)
    var ewsFunction = 'GetItem';
      var ewsArgs = {
        'ItemShape': {
          'BaseShape': 'Default',
          't:AdditionalProperties': {
            't:FieldURI': {
                'attributes': {
                'FieldURI': 'item:Attachments'
                }
            }
            }
          },
        'ItemIds' : {
          'ItemId': {
            'attributes': {
              'Id': Id,
              'ChangeKey' : ChangeKey
            }
          }
        }
      };
      await ews.run(ewsFunction, ewsArgs, ewsSoapHeader)
      .then(result => {
          // Iterate over the result and extract meesage
      })

推荐阅读