Archived Forum Post

Index of archived forum posts

Question:

MailMan Intermittent "Connection Closed By Peer" Error

Jan 23 '16 at 20:00

I am creating a MailMan object, unlocking, connecting to SMTP and am able to retrieve and send emails, however (seemingly sporadically) the DeleteEmail will not process because supposedly the connection closed itself?

Dim objPOP3 As New Chilkat.MailMan
Dim objBundle As Chilkat.EmailBundle
Dim objMessage As Chilkat.Email = Nothing

If (objPOP3.UnlockComponent(unlockKey) = True) Then
objPOP3.MailHost = ServerAddress
objPOP3.PopUsername = UserID
objPOP3.PopPassword = Password
objBundle = objPOP3.CopyMail

     If (objBundle IsNot Nothing) Then
          If (objBundle.MessageCount > 0) Then           
               For intMessageNumber = 0 To (objBundle.MessageCount - 1)
                    objMessage = objBundle.GetEmail(intMessageNumber)
                    ' Do stuff
                    objPOP3.SmtpHost = GetConfigValue("SMTPHost")
                    objPOP3.SmtpUsername = UserID
                    objPOP3.SmtpPassword = Password
                    objPOP3.ConnectTimeout = 600
                    objPOP3.ReadTimeout = 600

                    blnResult = objPOP3.SendEmail(objMessage) ' This returns true

                    If (blnResult = False) Then
                         ConsoleWrite("*An error occurred: *" + objPOP3.LastErrorText + "*")
                    Else
                         ' The code gets here, but objPOP3.DeleteEmail(objMessage) will return False
                         If (objPOP3.DeleteEmail(objMessage) = False) Then
                              ConsoleWrite("*An error occurred: *" + objPOP3.LastErrorText + "*")
                         End If
                    End If

LastErrorText:

*An error occurred: *ChilkatLog:
  DeleteEmail:
    DllDate: Aug  5 2012
    UnlockPrefix: UnlockPrefixHere
    Username: Servername:username
    Architecture: Little Endian; 32-bit
    Language: .NET 2.0
    VerboseLogging: 0
    uidl: UID58417-1355242373
    msgNum: 1
    PopCmdSent: DELE 1
    numBytesRequested: 2048
    Connection closed by connected peer.
    timeoutMs: 600000
    timedOut: 0
    aborted: 0
    fatalError: 0
    connectionClosed: 1
    Failed to received remainder of POP3 multi-line response.
    matchStr:

    message may not have been successfully marked for delete
    Failed.
  --DeleteEmail
--ChilkatLog

Now I can add in a (objPOP3.IsSmtpConnected = True) before attempting to delete, but I do not understand why the connection is closing as it isn't saying it timed out. What else would cause it to think the 'Connection was closed by peer'? If I perform a check to see if the connection is still open and it returns False, how would I re-establish the connection in order to attempt to delete? I tried to use objPOP3.Pop3BeginSession() to accomplish this, which does not work because my next error said there was no socket for sending. I assume this to be the result of the connection still being closed.

Last Error Text:

*An error occurred: *ChilkatLog:
  DeleteEmail:
    DllDate: Aug  5 2012
    UnlockPrefix: UnlockPrefixHere
    Username: servername:username
    Architecture: Little Endian; 32-bit
    Language: .NET 2.0
    VerboseLogging: 0
    uidl: UID58417-1355242373
    msgNum: 1
    No socket exists for sending (2b)
    Failed to send command
    command: DELE 1
    message may not have been successfully marked for delete
    Failed.
  --DeleteEmail
--ChilkatLog
*

To get the email to delete, I'll have to manually end our application and re-run it, which indicates that it is not a code error. There's seemingly no connection between the random emails that are not initially deleted, so I do not know how to pinpoint the initial cause. Any ideas on how to prevent going forward? Thanks!

01.14.2016 UPDATE

Has anyone been able to pinpoint the issue here? I am still running across this error periodically and have not been able to resolve. How would I go about reconnecting the session if needed?

Also, this morning I ran into a new situation where objPOP3.IsSmtpConnected returned true but the DeleteEmail that followed failed... Is there any other potential cause for the 'Connection closed by connected peer.' or 'No socket exists for sending (2b)' errors? Everything that I’ve read from your documentation seems to indicate that these errors are raised solely if the Connect method fails or is never called, so I am not sure why the Mailman object is saying it's connected then failing to delete...unless the Mailman object just happened to disconnect at the exact moment between my ConsoleWrite line and the delete method being called. See my log file snippet below.

1/14/2016 7:06:55 AM: * Attempt to delete email *
1/14/2016 7:06:55 AM: *Is Chilkat connected? -> True*
1/14/2016 7:06:55 AM: *An error occurred: *ChilkatLog:
  DeleteEmail:
    DllDate: Aug  5 2012
    UnlockPrefix: UnlockPrefixHere
    Username: Servername:username
    Architecture: Little Endian; 32-bit
    Language: .NET 2.0
    VerboseLogging: 0
    uidl: UID60462-1355242373
    msgNum: 1
    PopCmdSent: DELE 1
    numBytesRequested: 2048
    Connection closed by connected peer.
    timeoutMs: 600000
    timedOut: 0
    aborted: 0
    fatalError: 0
    connectionClosed: 1
    Failed to received remainder of POP3 multi-line response.
    matchStr:

    message may not have been successfully marked for delete
    Failed.
  --DeleteEmail
--ChilkatLog

The reason behind the disconnecting aside, HOW can I successfully re-establish the connection when this occurs?? In the event of a deletion failure, I have also resorted to trying to manually begin the session, setting ImmediateDelete to false, manually marking the message for delete, then manually ending the session which should finalize the deletion...but it appears my message wasn't successfully marked for delete for some reason. I guess my next question is what would cause a message to not be successfully marked for delete, but I'm assuming that the BeginSession did not work (I will need to add another check for the connection right after this), hence the no sockets found, hence the message couldn't be marked for delete...but why and how do I successfully reconnect?? Code and continuation of log file below:

' If it fails
                    ConsoleWrite("*Is Chilkat connected? -> " + objPOP3.IsSmtpConnected.ToString() + "*")
                    If (objPOP3 .IsSmtpConnected = True) Then
                        If (objPOP3.DeleteEmail(objMessage) = False) Then
                            ConsoleWrite("*An error occurred: *" + objPOP3.LastErrorText + "*")
                            ConsoleWrite("*Email failed to delete with ImmediateDelete set to true. Attempting to manually delete*")
                            ' Attempt to manually open connection, delete emails, and close POP3 connection
                            ConsoleWrite("*Opening POP3 connection*")
                            objPOP3.Pop3BeginSession()

                            ' Set ImmediateDelete to false - requires connection to close before it will delete
                            ConsoleWrite("*Setting ImmediateDelete to false*")
                            objPOP3.ImmediateDelete = False

                            ' Attempt to mark email for deletion
                            Dim marked As Boolean = objPOP3.DeleteEmail(objMessage)
                            ConsoleWrite("*objPOP3.DeleteEmail(objMessage) - marked: " + marked.ToString() + "*")

                            If marked = False Then               
                                    ' There was an error deleting email/closing connection
                                    ConsoleWrite("*An error occurred: *" + objPOP3.LastErrorText + "*")
                                    ConsoleWrite("*Email failed to be deleted or connection failed to close.*”)
                                End If

1/14/2016 7:06:55 AM: *Email failed to delete with ImmediateDelete set to true. Attempting to manually delete*
1/14/2016 7:06:55 AM: *Opening POP3 connection*
1/14/2016 7:06:55 AM: *Setting ImmediateDelete to false*
1/14/2016 7:06:55 AM: *objPOP3.DeleteEmail(objMessage) - marked: False*
1/14/2016 7:06:55 AM: *An error occurred: *ChilkatLog:
  DeleteEmail:
    DllDate: Aug  5 2012
    UnlockPrefix: UnlockPrefixHere
    Username: Servername:username
    Architecture: Little Endian; 32-bit
    Language: .NET 2.0
    VerboseLogging: 0
    uidl: UID60462-1355242373
    msgNum: 1
    No socket exists for sending (2b)
    Failed to send command
    command: DELE 1
    message may not have been successfully marked for delete
    Failed.
  --DeleteEmail
--ChilkatLog
*
1/14/2016 7:06:55 AM: *Email failed to be marked for delete in Chilkat.*

Any guidance would be much appreciated!


Accepted Answer

You are using an old version of Chilkat. Test with the latest version. It is likely the problem is resolved.