nmarus / node-ews

A simple JSON wrapper for the Exchange Web Services (EWS) SOAP API.
MIT License
116 stars 52 forks source link

How to read unread email with body and attachment #125

Open niti1405 opened 4 years ago

niti1405 commented 4 years ago

Hi,

I am able to send email using this 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://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 but the examples are provided in C#, C++ or VB..

But I want to do this with Nodejs.

sharma-akshay commented 4 years ago

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

thanushiya13 commented 4 years ago

Hi im getting attachementID,contenttype only.How to get attchment content to write ina file? @sharma-akshay