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

Aurangabadkar Sharvari 1 Reputation point
2020-08-20T22:18:39.733+00:00

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");

                }

Can I get a resolution for this issue?

Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,408 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,911 questions
{count} votes

2 answers

Sort by: Most helpful
  1. KyleXu-MSFT 26,211 Reputation points
    2020-08-21T02:09:27.407+00:00

    Please Note: The using of EWS is related with Exchange development which doesn't supported in Q&A Exchange forum, I would suggest you post it in Exchange development forum or open a ticket to Microsoft. It will help solve this problem.

    Here are some suggestions which may be useful to you:

    Which is the version of Exchange server that you want to connect to? Exchange on-premises or Exchange online?

    If you are connecting to Exchange on-premises, there are some limitation on the max connection:

    Limitation 1: "-EWSMaxConcurrency" in Throttling Policy:

    Get-ThrottlingPolicy | fl EWSMaxConcurrency  
    

    Limitation 2: "maxConnections" in EWS confirmation file: C:\Program Files\Microsoft\Exchange Server\V15\FrontEnd\HttpProxy\EWS\web.config

    If you are connection to Exchange online, as far as I know the limitation will cannot be changed, you need to wait for a while then try to connect to Exchange online again.

    Here is an article which also may be useful to you: Throttling considerations for applications that use EWS impersonation


    If the response is helpful, please click "Accept Answer" and upvote it.

    1 person found this answer helpful.
    0 comments No comments

  2. James 1 Reputation point
    2020-09-16T20:48:43+00:00
    0 comments No comments