private static void GetFullFilePath(DataLakeStoreFileSystemManagementClient client, FileStatusProperties fileStatusProperties, List <string> finalFilePathList, string path)
        {
            string fullPath = String.Format("{0}/{1}", path, fileStatusProperties.PathSuffix);

            if (fileStatusProperties.Type == FileType.DIRECTORY)
            {
                FileStatusesResult fileStatusesResult = client.FileSystem.ListFileStatus(accountName, fullPath);

                foreach (FileStatusProperties fsProp in fileStatusesResult.FileStatuses.FileStatus)
                {
                    GetFullFilePath(client, fsProp, finalFilePathList, fullPath);
                }
            }

            if (fileStatusProperties.Type == FileType.FILE)
            {
                if (finalFilePathList == null)
                {
                    finalFilePathList = new List <string>();
                }

                finalFilePathList.Add(fullPath);
            }
        }
        private static List <Message> GetMessages()
        {
            List <Message> messagesList = new List <Message>();

            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

            ServiceClientCredentials creds = ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, clientSecret).Result;

            DataLakeStoreFileSystemManagementClient client = new DataLakeStoreFileSystemManagementClient(creds);

            bool pathExists = client.FileSystem.PathExists(accountName, msgPath);

            if (!pathExists)
            {
                return(null);
            }

            FileStatusesResult fileStatusesResult = client.FileSystem.ListFileStatus(accountName, msgPath);

            if (fileStatusesResult == null || fileStatusesResult.FileStatuses == null || fileStatusesResult.FileStatuses.FileStatus == null)
            {
                return(null);
            }

            List <string> finalFilePathList = new List <string>();

            foreach (FileStatusProperties fileStatusProperties in fileStatusesResult.FileStatuses.FileStatus)
            {
                GetFullFilePath(client, fileStatusProperties, finalFilePathList, msgPath);
            }

            foreach (string filePath in finalFilePathList)
            {
                using (var stream = client.FileSystem.Open(accountName, filePath))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        string line;

                        while ((line = reader.ReadLine()) != null)
                        {
                            Message message = new Message();

                            message.ToRecipients  = new List <string>();
                            message.CcRecipients  = new List <string>();
                            message.BccRecipients = new List <string>();

                            JObject obj = JObject.Parse(line);

                            string sender = obj.SelectToken("Sender.EmailAddress.Address")?.ToString();

                            message.Sender = obj.SelectToken("Sender.EmailAddress.Address") == null?obj.SelectToken("From.EmailAddress.Address")?.ToString() : sender;

                            IEnumerable <JToken> val = obj.SelectTokens("ToRecipients");
                            foreach (JObject child in val.Children())
                            {
                                message.ToRecipients.Add(child.SelectToken("EmailAddress.Address")?.ToString());
                            }

                            val = obj.SelectTokens("CcRecipients");
                            foreach (JObject child in val.Children())
                            {
                                message.CcRecipients.Add(child.SelectToken("EmailAddress.Address")?.ToString());
                            }

                            val = obj.SelectTokens("BccRecipients");
                            foreach (JObject child in val.Children())
                            {
                                message.BccRecipients.Add(child.SelectToken("EmailAddress.Address")?.ToString());
                            }

                            if (sender != null)
                            {
                                messagesList.Add(message);
                            }
                        }
                    }
                }
            }

            return(messagesList);
        }