Pop3 error

Hello,


We are running an import from a windows service that reads an email from a pop3 server.
Sometimes when we connect to the server we get the following exception from aspose:

Non-negative number required. Parameter name: length
System.ArgumentOutOfRangeException: Non-negative number required. Parameter
name: length at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array
destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) at
Aspose.Network.Security.x3793faac0c4d40eb.x08106e88700f5b14(Byte[]
x5cafa8d49ea71ea1, Int32 x374ea4fe62468d0f, Int32 x10f4d88af727adbc) at
Aspose.Network.Security.x56bf2af7cd30a799…ctor(x62e74ca3e30e55c2
protocolVersion, x99451997b5ffb8e6 allowedSuites, Byte[] sessionID, Int32
prefferedSuite) at Aspose.Network.Security.x2b0895eb7eb6f124.BeginHandshake() at
Aspose.Network.Security.x7733a6eb08815e0c.xaa72a8fac2b6244a() at
Aspose.Network.Pop3.x273b0563d5729a6f.xe79d8fda991f8eac(x6d3bf3892136234b
x874e1670b98e53ec) at Aspose.Network.Pop3.Pop3Client.Connect(Boolean autoLogin)
at Aspose.Network.Pop3.Pop3Client.Connect(Boolean autoLogin) at

We use the following code to connect to the server:

Pop3Client server = new Pop3Client(Settings.Default.PopMailServerAddress, Settings.Default.PopMailServerPort, Settings.Default.PopMailUserName, Settings.Default.PopMailPassword);
server.EnableSsl = Settings.Default.PopMailUseSsl;
server.SecurityMode = Pop3SslSecurityMode.Implicit;
server.Connect(true);
server.Login();

Our server url en credentials are stored in the app.config.The error is gone after we restart the service.
Why are we getting this exception?

Hi,

Thanks for considering Aspose.

Could you please provide the following details in order to further analyze this problem.

  1. Are you using Pop3Client.Disconnect() method after importing the emails?
  2. How many times in an hour the service connects to the Pop3 server?
  3. How frequently (per day) do you get this exception?

Hi Saqib,

1 We are using disconnect, I just moved it to a finally block so it will always be called.
This can explain the behaviour we have right now
2 This is a base class for 5 classes. some tasks run every 5 minutes.
3 Every time we call the connect.

For now i will update the software with the disconnect in the finally block and i will get back tomorrow to let you known if this was the problem. For now thanks for pointing in the right direction.

Hi Saqib,

As we said we moved the disconnect to the finally block, but the problem still occassionally occurs.
Here is the code snippet.
Could you please provide a further solution to this problem?
Many thanks in advance.

try
{
ConnectionId = ConnectionManager.CreateConnection(this, false);

if(!String.IsNullOrEmpty(filePath))
{
logEntity.Success = ProcessFile(filePath);
}
else
{
License license = new License();
license.SetLicense("Aspose.Total.lic");

Pop3Client server = new Pop3Client(Settings.Default.PopMailServerAddress, Settings.Default.PopMailServerPort, Settings.Default.PopMailUserName, Settings.Default.PopMailPassword);
server.EnableSsl = Settings.Default.PopMailUseSsl;
server.SecurityMode = Pop3SslSecurityMode.Implicit;
try
{
server.Connect(true);
server.Login();

Pop3MessageInfoCollection messagesInfo = server.ListMessages(false);

foreach(Pop3MessageInfo messageInfo in messagesInfo)
{
try
{
if(Regex.IsMatch(messageInfo.Subject, Subject))
{
//Get mailmessage from server, this also automatically deletes the mail message from the server
MailMessage message = server.FetchMessage(messageInfo.SequenceNumber);

// process mail
}
else
{
//log
}
}
catch(Exception ex)
{
// log
}
}
}
finally
{
server.Disconnect();
}
}
}
catch(Exception ex)
{
// log
}
finally
{
// If the task ends without a success then try again in 30 minutes.
// This can be when there is an exception or the downloadfile isn't updated yet.
}

Hi,

Thanks for the required information.

I will also create a windows service and check pop3 ssl mailbox in similar scenario to check the issue.

The code snippet looks fine to me, but there is one thing (although it should not cause this problem) that license is being set every time you connect to the mailbox. Could you please move the license setting code to init() or OnStart() method, so that the license is set only once per process/application?

I will get back to you with results at my end.

We use asynchronous delegate methods, so the same code can be run for multiple "tasks" in theory at exactly the same time.
Could that cause the problem wit connecting/disconnnecting to the mailbox?
In that case should we maybe check if the mailbox state is open or something similar?

Will the license apply to the whole application if set in the OnStart metohd of the windows TaskService?

ETTU:

We use asynchronous delegate methods, so the same code can be run for multiple "tasks" in theory at exactly the same time.
Could that cause the problem wit connecting/disconnnecting to the mailbox?
In that case should we maybe check if the mailbox state is open or something similar?

We are working on this scenario at our end.

ETTU:

Will the license apply to the whole application if set in the OnStart metohd of the windows TaskService?

If you are using Aspose.Network only in the service, then license will be applied to all classes that are being called in the service.

But, if you are also using another desktop/web based application and also using Aspose.Network classes in these, then you need to set licenses in the init() or global.asax's Application_OnStart() methods.

Hi we are also trying to reproduce the issue without result so far, but the problem consistently keeps recurring every now and then.
Did you have any luck on your side yet?
Here's the stacktrace again:

Parameter name: length

System.ArgumentOutOfRangeException: Non-negative number required.

Parameter name: length

at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)

at Aspose.Network.Security.x3793faac0c4d40eb.x08106e88700f5b14(Byte[] x5cafa8d49ea71ea1, Int32 x374ea4fe62468d0f, Int32 x10f4d88af727adbc)

at Aspose.Network.Security.x56bf2af7cd30a799..ctor(x62e74ca3e30e55c2 protocolVersion, x99451997b5ffb8e6 allowedSuites, Byte[] sessionID, Int32 prefferedSuite)

at Aspose.Network.Security.x2b0895eb7eb6f124.BeginHandshake()

at Aspose.Network.Security.x7733a6eb08815e0c.xaa72a8fac2b6244a()

at Aspose.Network.Pop3.x273b0563d5729a6f.xe79d8fda991f8eac(x6d3bf3892136234b x874e1670b98e53ec)

at Aspose.Network.Pop3.Pop3Client.Connect(Boolean autoLogin)

at Aspose.Network.Pop3.Pop3Client.Connect(Boolean autoLogin)

at KasEnergy.EBS.Service.Tasks.MailBaseTask.Run(TaskManager parent, ServiceTaskEntity task, String filePath) .

Hi,

Most probably, there is an issue with concurrent access to the pop3 mailbox. We will investigate this issue further at our end. In the meanwhile, could you please try the following alternate method?

private bool ConnectToMailbox(Pop3Client client)
{
try
{
// try to connect to pop3 mailbox
client.Connect(true);
// return true, if successfully connected
return true;
}
catch (Exception)
{
// return false
return false;
}
}
}

Use this method where you are initiating a connection to pop3 mailbox as follows:

Pop3Client client = new Pop3Client();
// set host, port, user, pwd, ssl etc
while (ConnectToMailbox(client) == false)
{
Thread.Sleep(5000);
}
// do emails processing
Pop3MessageInfoCollection msgInfoColl = client.ListMessages(); // etc…

This way, if a user is already connected to the mailbox, the program will pause for 5 sec and will try again connecting to mailbox.

Please try this approach and see if exceptions are still thrown or not. This issue is also logged in our bug tracking system (ID: 13467). I will update you with our findings soon. Sorry for the inconvenience caused.

Thank you, we have implemented this and are checking the results.
But we think this won't be the solution, because when the error occurs exceptions will be thrown untill we restart the whole windows service.
It looks like there is no way of reconnecting again.

After half an hour we are going to disconnect and try to reconnect again.

Hi saqib,

The issue is still occurring. Sometimes it manages to reconnect after 5 seconds.
But sometimes it will collapse and cannot get out of a Closed Connection state.
In a quite complex enterprise system, this issue is actually the only issue in our system that has still proved "unsolvable" for us, and the only structural one left.

Help is appreciated a lot.

Regards.

Hi,

We will do some further analysis and get back to you.