Android Question POP3 Not Reading Messages (SOLVED)

MaxWaterfall

Member
Licensed User
Longtime User
Okay I'm using pop3 for a gmail account in my application.


B4X:
Sub POP_ListCompleted (Success As Boolean, Messages As Map)
   If Success Then
        'If the list completed was a success then all the messages in the map of messages will be downloaded
        For i = 0 To Messages.Size - 1
            POP.DownloadMessage(Messages.GetKeyAt(i), True) 'Download all messages
        Next
        'The number of files recieved should be exactly 181
        If FilesRecieved = 181 Then
         Msgbox("Data Version: " & File.ReadString(File.DirDefaultExternal, "DataVersion"),"Files Recieved Successfully")
         Activity.finish
         StartActivity(Main)
        Else
          Msgbox("An error has occured, an incorrect amount of files has been recieved, re-send the data.", "Files Recieved Unsuccessfully")
        End If
    Else
        Msgbox("An error has occurred", "")
    End If
    POP.Close 'The connection will be closed after all messages are downloaded
End Sub


It gets to this sub but when it reaches the "For i = 0 to..." it skips it as there is no messages in the map. I do not understand as in the gmail account there are many messages. Any ideas?
 

DonManfred

Expert
Licensed User
Longtime User
without seeing your code it is hard to give congrete answers.
What is the content of your
B4X:
Sub pop_DownloadCompleted (Success As Boolean, MessageId As Int, Message As String)
sub?
 
Upvote 0

MaxWaterfall

Member
Licensed User
Longtime User
It's irrelevant as the
B4X:
POP.DownloadMessage(Messages.GetKeyAt(i), True)
is never even run. The problem is to do with the messages. The map made from the list contains no messages.
However, here is the content of
B4X:
Sub pop_DownloadCompleted (Success As Boolean, MessageId As Int, Message As String)
anyway:
B4X:
Sub POP_DownloadCompleted (Success As Boolean, MessageId As Int, MessageText As String)
   ' Log("Download: " & Success & ", " & MessageId)
   Dim SearchForDeviceName As String
  
    If Success Then
       'If the download is a success then parse the mail to a message format and save it to the device
       m = MailParser.ParseMail(MessageText, File.DirDefaultExternal)
     
      SearchForDeviceName = m.Subject
      'Searches for the device name within the subject
      If SearchForDeviceName.Contains(DeviceName) Then
         'A mail can only be deleted before it is downloaded so I use a second POP3 that downloads and
         'deletes the messages based on their MessageID
         Delete = MessageId
        ' POPDelete.ListMessages
      Else
         Return
      End If
      'The desktop application will send emails in this format
      If m.Subject = DeviceName & "DataVersion" Then
         'Write body of email containg the version of the data being sent to the device to the device
         File.WriteString(File.Dirdefaultexternal, "DataVersion", m.Body)
         'Counts how many files the device has recieved to check it has recieved the correct amount
         FilesRecieved = FilesRecieved + 1
         'Exit the sub to save time by not executing the rest of the code within the sub
         Return
      End If
     
      'The following for loops are used to check what data is being sent
     
      '15 users
       For x = 1 To 15
             'DeviceName is chosen by the user and allows the user to send data to more than one device
               If m.Subject = DeviceName & "UserName" & x Then
                'Write body of the email (contains the data) to the device
                   File.WriteString(File.DirDefaultExternal, "UserName" & x & ".txt", m.Body)   
                FilesRecieved = FilesRecieved + 1
                Return
               End If
            If m.Subject = DeviceName & "UserNumber" & x Then
                File.WriteString(File.DirDefaultExternal, "UserNumber" & x & ".txt", m.Body)
                FilesRecieved = FilesRecieved + 1
                Return
            End If
            If m.Subject = DeviceName & "UserAdmin" & x Then
                File.WriteString(File.DirDefaultExternal, "UserAdmin" & x & ".txt", m.Body)
                FilesRecieved = FilesRecieved + 1
                Return
            End If
       Next
          
       '60 Products
       For x = 1 To 60
            If m.Subject = DeviceName & "ProductName" & x Then
                File.WriteString(File.DirDefaultExternal, "ProductName" & x & ".txt", m.Body)
                FilesRecieved = FilesRecieved + 1               
                Return
            End If
            If m.Subject = DeviceName & "ProductPrice" & x Then
                File.WriteString(File.DirDefaultExternal, "ProductPrice" & x & ".txt", m.Body)
                FilesRecieved = FilesRecieved + 1               
                Return
            End If
       Next
    Else 'Success not true
       Return   
    End If
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It has something to do with Gmail configuration. The server treats all messages as if they were already downloaded so it doesn't return any "new" message.

See this link: https://support.google.com/mail/answer/47948

Note that your code is wrong. It will not download the messages correctly. But first you should solve the list issue.
 
Upvote 0

MaxWaterfall

Member
Licensed User
Longtime User
Okay awesome thankyou, it is now downloading all messages. But like you said my code is wrong,
POP_DownloadCompleted is not being called after each message has downloaded?
 
Upvote 0
Top