Socket Usage


I am using the Exchange client to connect to exchange 2003. Where possible I am using the same exchange client to connect to exchange and get the information back.

However after a while I get the following message:

System.Net.WebException: Unable
to connect to the remote server —> System.Net.Sockets.SocketException:
Only one usage of each socket address (protocol/network address/port) is
normally permitted

at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)

at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

--- End of inner exception stack trace ---

at System.Net.HttpWebRequest.GetResponse()

at Aspose.Email.Exchange.ExchangeClient.ListMessages(String folder, Int32 maxNumberOfMessages, ExchangeListMessagesOptions options)

at Aspose.Email.Exchange.ExchangeClient.ListMessages(String folder,
ExchangeListMessagesOptions options)

The method the error occurs on does vary sometimes from the ListMessages to the FetchMessage one, but it is always on the client call.

Is there any way I can stop this error occurring?


Hi David,

We are sorry for your trouble.

Can you please provide us a sample console application exhibiting the behaviour as you have described it. We will look into it very soon.

Also, you can test your scenario with latest version of Aspose.Email for .NET v1.2.0 and post your results here for our review.

Thank you for your cooperation.


I have tried it with the latest Aspose.Email and unfortunately the issue is still happening. I have created come simple code that will can reproduce the issue.

Some additional information for you first if this helps: the issue can be reproduced on a XP SP3 and Windows 2003 SP2 Server machine, however it can not be reproduced on a Windows 2008 or Windows 7 machine. It also doe not occur if it is run locally on the exchange 2003 server (assume it is running the equivalent of localhost and therefore the sockets are treated differently??).

The code below is what I have used to test and reproduce the issue. The folder I am accessing a folder for the user specified (in this case administrators) and has 951 e-mail messages in it and getting it to do the retrieval 10 times. The error occurs (pretty consistently) on the 5 iteration of retrieving the data.

It is also worth noting that this is just a test, in the original program I am encountering the error with I am not iterating over the same folder, but every folder in the mailbox (there are about 70 folders total, and ~60k items). The original program fails after about 5 or 6 folder retrievals as well.


int count = 0;



for (int i = 0; i < 10; i++)


using (ExchangeClient client = new ExchangeClient(@“http://autobvt-xchangy/exchange”, “administrator”, “apples1!”, “exchange”))


client.KeepAlive = false;

ExchangeMessageInfoCollection messages = client.ListMessages(@“http://autobvt-xchangy/exchange/administrator/Inbox/Test%20Folder%201/Top%20Level%20Folder/”, ExchangeListMessagesOptions.FetchAttachmentInformation);

count = count + messages.Count;



     <span style="color: rgb(43, 145, 175); ">MessageBox</span>.Show(<span style="color: rgb(163, 21, 21); ">"Completed: "</span> + count + <span style="color: rgb(163, 21, 21); ">" messages retrieved"</span>);</pre><pre style="font-family: Consolas; font-size: 13px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: white; background-position: initial initial; background-repeat: initial initial; ">      }
  <span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)
      <span style="color:#2b91af;">MessageBox</span>.Show(<span style="color:#a31515;">"An error occurred ("</span> + count + <span style="color:#a31515;">" messages retrieved): "</span> + ex.ToString());
  }</pre></div><div>----------------------------------------------------------------------------------------</div><div><br></div><div><br></div><div>The keepalive setting was something I added in along the way to se


It is also worth noting that we are in dire need of a resolution for this as we need this for a customer project being deployed next Friday (16th Dec). I am sure everyone on the forum has the same urgency but just wanted to let you know :slight_smile:


Hi David,

Thank you for your analysis and sample code.

We are trying to replicate the said issue on Windows XP and Windows Server 2003 platforms by connecting to the Aspose exchange server. The maximum items are no more than ~300 but we will try again with a larger inbox as well.

By checking you code, we have noticed that the cause of your problem may be the way you are accessing your mailbox. We suggest the two following approaches,

  • Either you can specify the mailbox in ExchangeClient constructor like
client = new ExchangeClient(**“http[s]://server:port/exchange//”** ,userName, password, domain)
  • Or if you define only exchange server in the constructor, you need to specify the mailbox in GetMailboxInfo method, like
client = new ExchangeClient(**[“https://exchange.test.server”](https://exchange.test.server/)**,
userName, password, domain);

ExchangeMailboxInfo mbInfo = client.**GetMailboxInfo(“Test1”)**;
ExchangeMessageInfoCollection messages = client.ListMessages(mbInfo.InboxUri);

Can you please change the code in your main application and test if you could avoid the exception with any of above mentioned approaches. That will save time for both of us.

Thank you for your understanding and cooperation.


Just to let you know I have worked around this issue. The basics of the problem are the number of socket connections that are made by the Aspose ExchangeClient when getting back the lists of items. It seems to add a new socket connection for each mail item retrieved. By default (on Windows Server 2003 and XP anyway) the default max sockets that can be opened are 5000. They also have a default timeout before freeing up the port of 5mins. This means that when retrieving a large number of mail items the ports are quickly used up and the error occurs.

I have increased the allowed port count and reduced the timeout period so that the ports are freed up quicker. The steps for doing this is are detailed in the MSDN article here:

This seemed to avoid the issue, however I am not sure if there is something that could be done longer term with in Aspose that could either re-use the sockets or get them to close better?


Hi David,

Good to know that your issue is resolved and many thanks for posting your solution on the forum.

I will discuss this matter with the development team. If required, I will also log a ticket to provide a long term solution for this issue.

We will keep you posted with updates.

Thank you,