0

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 e.g. 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.

1 Answer 1

0

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
      })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.