Android Question Mailparser retrieves messages from server out of sequence

William Hunter

Active Member
Licensed User
The Mailparser retrieves messages from the mail server out of sequence, according to the order of MessageId as listed in the log.

I don’t know if this is something that can be resolved in Mailparser, or if it is a characteristic of the mail server as to how it indexes messages.

This anomaly becomes a problem when deleting messages from the server, as message two and three are in reverse order. What I see as message two is actually message three, and message three is actually message two. Any deletion I make is made using the numbered order of the messages as displayed in the app. The anomaly occurs solely with these two messages.

This is the Log:
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=11, TotalSize=58505
Messages.Size = 11
MessageID = 1
MessageID = 3
MessageID = 2
MessageID = 4
MessageID = 5
MessageID = 6
MessageID = 7
MessageID = 8
MessageID = 9
MessageID = 10
MessageID = 11
** Activity (main) Pause, UserClosed = true **

This is my code:
B4X:
Sub Activity_Create(FirstTime AsBoolean)
  If FirstTime Then
    pop.Initialize("pop.mailserver.com", 110, "username", "password", "pop")
    'pop.UseSSL = True ' uncomment if SSL required
    pop.Status
    listSubject.Initialize
    listFrom.Initialize
  EndIf
  Activity.LoadLayout("Start")
  pop.ListMessages
End Sub

Sub POP_StatusCompleted (Success AsBoolean, NumberOfMessages AsInt, TotalSize AsInt)
  messageCount =   NumberOfMessages
  Log("Success=" & Success & ", NumberOfMessages=" & NumberOfMessages & ", TotalSize=" & TotalSize)
End Sub

Sub POP_ListCompleted (Success AsBoolean, Messages AsMap)
  If Success = FalseThen
    Log(LastException.Message)
  Else
    'download all messages
    'change last parameter to True if you want to delete the messages from the server
    Log("Messages.Size = " & Messages.Size)
    For i = 0 To Messages.Size - 1
       pop.DownloadMessage(Messages.GetKeyAt(i), False)
    Next 
  EndIf
  pop.Close
End Sub

Sub POP_DownloadCompleted (Success AsBoolean, MessageId AsInt, MessageText AsString)
  If Success = FalseThen
     Log("LastException = " & LastException.Message)
  Else
     'Parse the mail
     Dim M AsMessage
     M = MailParser.ParseMail(MessageText, File.DirRootExternal)
     'Log("M = " & M)
     Log("MessageID = " & MessageId)
     'Log("Subject = " & M.Subject)
     listSubject.Add(M.Subject)
     listFrom.Add(M.FromField)
     'Log("listFrom.Size = " & listFrom.Size)
     'Log("MessageCount = " & messageCount)
     If listFrom.Size = messageCountThen
        CreateScrollView
     EndIf
  EndIf
End Sub

Would someone with a better understanding of how mail servers work comment on this. Is this normal behavior? Is there some means of compensating for this when retrieving messages? Any enlightenment will be greatly appreciated.

Best regards :)
 
Last edited:

William Hunter

Active Member
Licensed User
What is the output of Log(Messages)?
Thank you for your response. I have now included the output of Log(Messages) in the log.

I have listed the Logs for nine consecutive connections to the pop3 server. For the first three connects the messages are downloaded in the correct order. Then for the next three connects messages two and three are downloaded in reverse order. Then for the last three connects the messages are again downloaded in the correct order.

I was not pushing the server, giving it some recovery time after pop.Close. I can’t see a cause for this erratic behavior in my code. Do you have any thoughts? :confused:
B4X:
LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=5, TotalSize=5416
Messages.Size = 5
Messages = (MyMap) {1=1082, 2=1082, 3=1086, 4=1084, 5=1082}
i = 0
i = 1
i = 2
i = 3
i = 4
MessageID = 1
MessageID = 2
MessageID = 3
MessageID = 4
MessageID = 5
** Activity (main) Pause, UserClosed = true **


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=5, TotalSize=5416
Messages.Size = 5
Messages = (MyMap) {1=1082, 2=1082, 3=1086, 4=1084, 5=1082}
i = 0
i = 1
i = 2
i = 3
i = 4
MessageID = 1
MessageID = 2
MessageID = 3
MessageID = 4
MessageID = 5
** Activity (main) Pause, UserClosed = true **


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=5, TotalSize=5416
Messages.Size = 5
Messages = (MyMap) {1=1082, 2=1082, 3=1086, 4=1084, 5=1082}
i = 0
i = 1
i = 2
i = 3
i = 4
MessageID = 1
MessageID = 2
MessageID = 3
MessageID = 4
MessageID = 5
** Activity (main) Pause, UserClosed = true **


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=5, TotalSize=5416
Messages.Size = 5
Messages = (MyMap) {1=1082, 2=1082, 3=1086, 4=1084, 5=1082}
i = 0
i = 1
i = 2
i = 3
i = 4
MessageID = 1
MessageID = 3
MessageID = 2
MessageID = 4
MessageID = 5
** Activity (main) Pause, UserClosed = true **


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=5, TotalSize=5416
Messages.Size = 5
Messages = (MyMap) {1=1082, 2=1082, 3=1086, 4=1084, 5=1082}
i = 0
i = 1
i = 2
i = 3
i = 4
MessageID = 1
MessageID = 3
MessageID = 2
MessageID = 4
MessageID = 5
** Activity (main) Pause, UserClosed = true **


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=5, TotalSize=5416
Messages.Size = 5
Messages = (MyMap) {1=1082, 2=1082, 3=1086, 4=1084, 5=1082}
i = 0
i = 1
i = 2
i = 3
i = 4
MessageID = 1
MessageID = 3
MessageID = 2
MessageID = 4
MessageID = 5
** Activity (main) Pause, UserClosed = true **


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=5, TotalSize=5416
Messages.Size = 5
Messages = (MyMap) {1=1082, 2=1082, 3=1086, 4=1084, 5=1082}
i = 0
i = 1
i = 2
i = 3
i = 4
MessageID = 1
MessageID = 2
MessageID = 3
MessageID = 4
MessageID = 5
** Activity (main) Pause, UserClosed = true **


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=5, TotalSize=5416
Messages.Size = 5
Messages = (MyMap) {1=1082, 2=1082, 3=1086, 4=1084, 5=1082}
i = 0
i = 1
i = 2
i = 3
i = 4
MessageID = 1
MessageID = 2
MessageID = 3
MessageID = 4
MessageID = 5
** Activity (main) Pause, UserClosed = true **


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Success=true, NumberOfMessages=5, TotalSize=5416
Messages.Size = 5
Messages = (MyMap) {1=1082, 2=1082, 3=1086, 4=1084, 5=1082}
i = 0
i = 1
i = 2
i = 3
i = 4
MessageID = 1
MessageID = 2
MessageID = 3
MessageID = 4
MessageID = 5
** Activity (main) Pause, UserClosed = true **
Best regards
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
The logs are good. I wanted to make sure that the messages are in the correct order in the map.

The solution is to download the messages one after another instead calling pop.DownloadMessage for all of them at once. The download speed will be the same as POP protocol handles the messages one after another.

Store the map or messages in a process global variable together with the index variable.
Download the first message and then download the next message in Pop_DownloadCompleted event.

This way you will have full control over the order.
 
Upvote 0
Top