OfficeDev / ews-managed-api

Other
585 stars 321 forks source link

"Too many concurrent connections opened" errors with EWS OAuth in C# #256

Open sauranga2593 opened 4 years ago

sauranga2593 commented 4 years ago

Hello,

I am using a async function which enables EWS OAuth for mailbox. It tries to access mail folders and reads emails. While calling the function, I am facing following error: "too many concurrent connections opened. cannot open mailbox"

Here is a code sample I am using:

ewsClient.Url = new Uri(ewsClientURL); ewsClient.Credentials = new OAuthCredentials(result.AccessToken);

         //Console.WriteLine(result.AccessToken);
         //Impersonate the mailbox you'd like to access.
         ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress,ewsLogin);
         // Make an EWS call
         Mailbox mb = new Mailbox(rootFolderName); 
         Folder inbox = Folder.Bind(ewsClient, WellKnownFolderName.Inbox);
         List<Folder> subFolders = inbox.FindFolders(new FolderView(folderCount)).ToList();
         FolderId fid = null, archiveFid = null;
         foreach (Folder folder in subFolders)
         {
             if (folder.DisplayName.Equals(subFolderName))
             {
                 fid = folder.Id;
                 break;
             }
         }
         if (fid == null)
         {
             fid = new FolderId(WellKnownFolderName.Inbox, mb);
         }
         foreach (Folder folder in subFolders)
         {
             if (folder.DisplayName.Equals(archiveFolder)) 
             {
                 archiveFid = folder.Id;
                 break;
             }
         }
         if (archiveFid == null)
         {
             archiveFid = new FolderId(WellKnownFolderName.Inbox, mb);
         }
         //this will bind the mailbox you're looking for using your service instance
         Folder targetFolder = Folder.Bind(ewsClient, fid);
         //load items from mailbox inbox folder
         EmailMessage email = null;
         MessageBody body = null;
         if (targetFolder != null)
         {
             PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);
             itempropertyset.RequestedBodyType = BodyType.Text;
             ItemView itemview = new ItemView(emailCount);
             itemview.PropertySet = itempropertyset;
             FindItemsResults<Item> items = targetFolder.FindItems(itemview);
             StringBuilder sb = new StringBuilder();
             sb.Append("fileNames,emailSubject,Sender,UniqueId");
             foreach (var item in items)
             {
                 item.Load(itempropertyset);
                 email = EmailMessage.Bind(ewsClient, item.Id);
                 body = item.Body;
                 Guid id = Guid.NewGuid();
                 email.Move(archiveFid);
                 sb.Append("\n" + fileLocation + id.ToString() + ".txt" + "," + email.Subject + "," + email.From.Address + "," + email.InternetMessageId);
                 System.IO.File.WriteAllText(fileLocation + id.ToString() + ".txt", body.Text + "\n" + "DateReciept start:" + item.DateTimeReceived.ToUniversalTime() + "DateReciept end");

             }

What can be a resolution for this issue?

T33d33 commented 3 years ago

We had the same issues with connecting to EWS it seems to be a throttling problem. You are not allowed to make too many connections and you cannot change the threshold at what this error occures only way to solve this problem is to create an user pool and impersonate as different user for every request..