Unhandled System.IO.IOException

Using version of Aspose.Email on .NET 4.0 the program encounters an unhandled IOException occasionally.

The stack trace reported by Windows Error Reporting (WER) is:

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
at System.Net.Security._SslStream.ProcessWrite(Byte[], Int32, Int32, System.Net.AsyncProtocolRequest)
at System.Net.Security.SslStream.Write(Byte[], Int32, Int32)
at œ..Flush()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()

Note that the code and thread using AsposeEmailClient is not getting this exception.
The error stack appears to indicate the occurred in the context of an asynchronous operation that is calling Flush().

We are not explicitly using asynchronous calls such as BeginConnect/EndConnect, BeingSelectFolder/EndSelectFolder etc.

The ImapClient is used in the following way:

///This code runs periodically every couple of minutes.
try {
if(Connect()) {
_imapClient.SelectFolder(_inboxFolderName, false);
catch(Exception exception) {

public bool Connect() {
try {
if (_imapClient != null && _stopwatch.Elapsed.TotalMinutes < ImapConnectionExpirationPeriodInMins) {
return true; //ImapConnectionExpirationPeriodInMins is 30 minutes
if (_imapClient != null)
_imapClient = new ImapClient { Host = _serverName, Port = _portNumber, Username = _userName, Password = _password, SecurityMode = ImapSslSecurityMode.Implicit, EnableSsl = true, Timeout = Timeout };
return true;
catch (Exception exception) {
DumpStack(LogPrefix + "Connect() encountered exception: " + exception.Message, exception); return false;

public void Disconnect()
if (debug) Log(LogPrefix + "Imap Disconnect() Begin...");
if (_imapClient != null) {
try {
catch (Exception e) {
DumpStack(LogPrefix + "Disconnect() encountered exception: " + e.Message, e);
finally {
_imapClient = null;
if (debug) Log(LogPrefix + "Disconnect() Done...");

Note: this works most of the time, but periodically there is an unhandled IOException causing the process to terminate.

Note: It also seems that this error may not have been occurring originally when using version of Aspose.Email and .NET 2.0.

Does Flush() get called asynchronously?
What is the recommendation for recycling IMAP connections using a ImapClient object? Presently we are getting a new connection (after disconnecting) every 30 mins, is that too frequent or perhaps not frequent enough?

Hi Shyam,

Thank you for writing to Aspose support.

I would like to share that we have recently published Aspose.Email for .NET 3.9.0 that includes a number of enhancements to the ImapClient including keeping the connection open indefinitely once it is connected. In addition, this release also includes enhancements to the asynchronous operations using ImapClient. Since you are using quite an old version of Aspose.Email (3.6.0), I would request you to please try it with this latest version of Aspose.Email for .NET.

If the issue still persists, please provide us with a sample console application that we can use at our end for reproducing this issue and assist you further. We appreciate your cooperation in this regard.

Thank you for the quick response.
Based on your recommendation we are upgrading to version 3.9.0 of Aspose.Email.

We believe that as a side effect to invoking one (ore more) of the ImapClient methods (listed below), new thread(s) are starting and when an exception (such as System.IO.IOException) occurs on these thread(s) it is not caught (may be for good and legitimate reasons) and not handled causing our process to crash due to unhanded exception; which is server process handling client requests, resulting in unavailability of the server.

Could you please verify for us whether a new thread is started within any of the public ImapClient methods listed below?
ImapClient.SelectFolder(foldername, false)
ImapClient.AddMessageFlags(messageId, ImapMessageFlags.Deleted)
ImapClient.CopyMessage(messageId, folderName)

Is there a configuration setting or another way for the calling code to specify that new threads NOT be started? ie to perform operations synchronously. If not, could such an enhancement be added to the Aspose.Email library.
If a thread must be started, could you please verify that all Exceptions encountered on the thread are caught. This is very critical for us because such unhandled exceptions result in our server process crashing.

Hi Shyam,

I would like to share that I have tried to re-produce this issue but could not succeed. All these functions are synchronous and no separate thread is created for them. For further investigation, could you please provide a working console application along with the test account credentials, which can be used here to re-produce the scenario? It will help us to analyze the problem and provide solution as soon as possible.

Thanks you for your cooperation and understanding in this regard.