简体   繁体   中英

How to read unread email with body and attachment using node-ews package

I am able to send email using this node-ews package but I am not able find suitable example to read email from Inbox folder and get the body and attachments from the email.

I have gone through the Microsoft docs eg https://learn.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 but the examples are provided in C#, C++ or VB..

But I want to do this with Nodejs.

You can use following code to get emails from Inbox using FindItem function and then read each email using GetItem function

 // 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
      })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM