MailParser OutOfBounds error.

Mickster

Active Member
Licensed User
Longtime User
Hey Guys,

I'm using the mail parser module to try and download an attachment from some emails I have. I'm getting an index out of bounds error, though.

B4X:
index = Mail.IndexOf2("--" & boundary, index)

At this point, the index is returning a -1 as it can't locate the boundary. This causes an error when the code reaches

B4X:
Sub ReadNextLine (Mail As String)
   Dim sb As StringBuilder
   sb.Initialize
   Dim c As Char
   Do While index < Mail.Length
      c = Mail.CharAt(index)
      index = index + 1
      If c = Chr(13) OR c = Chr(10) Then
         If c = Chr(13) AND index < Mail.Length AND Mail.CharAt(index) = Chr(10) Then
            index = index + 1
         End If
         Exit 'break the loop
      End If
      sb.Append(c)
   Loop
   Return sb.ToString
End Sub

Does anyone have any suggestions?
 

Mickster

Active Member
Licensed User
Longtime User
I'm fairly sure that the message I'm receiving is a lot shorter than it should be.
 

Attachments

  • MessageText.txt
    582 bytes · Views: 226
Upvote 0

Mickster

Active Member
Licensed User
Longtime User
I managed to download the entire message, but the mailparse module isn't downloading the attachment. I can see in the messagetext that it's there this time, it's called zip.zip, yet the list of attachments is always empty.

Any thoughts?

B4X:
Sub pop_DownloadCompleted (Success As Boolean, MessageId As Int, Messages As String)
   
   If Success Then
      If Counter < Holder.Size - 1 Then
         Counter = Counter + 1
         Dim m As Message
         m = MailParse.ParseMail(Messages, File.DirRootExternal & "/Phenix/")
         Log("Downloaded: " & m.Subject)
         Log("Attachments: " & m.Attachments)
         File.WriteString(File.DirRootExternal & "/Phenix/", "MessageText.txt", Messages)
         pop.DownloadMessage(Holder.GetKeyAt(Counter), False)
      Else If Counter = Holder.Size -1 Then
         Msgbox("All Downloads finished","")
      End If
   Else
      Msgbox("Download of ind messages SUCKED", "")
   End If
   
End Sub
 

Attachments

  • MessageText.txt
    2.1 KB · Views: 227
Upvote 0

Mickster

Active Member
Licensed User
Longtime User
B4X:
Sub ParseMultipartBody (Mail As String, Msg As Message)
   'find first boundary
   index = Mail.IndexOf2("--" & boundary, index)
   ReadNextLine(Mail)
   Dim headers As StringBuilder
   headers.Initialize
   Do While index < Mail.Length
      Dim line As String
      line = ReadNextLine(Mail)
      If line.Length > 0 Then
         headers.Append(line).Append(" ")
      Else If index < Mail.Length Then
         Dim nextPart As Int
         nextPart = Mail.IndexOf2("--" & boundary, index)
         If nextPart-4 > index Then
            HandlePart(headers.ToString, Mail.SubString2(index, nextPart-4), Msg)
         End If
         If nextPart = -1 Then Return
         index = nextPart
         ReadNextLine(Mail)
         headers.Initialize
      End If
   Loop
End Sub

'nextpart' always equals 2 more than 'index', so the handlepart routine is never run. The handlepart routine deals with attachments, right?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try changing this sub to:
B4X:
Sub ParseMultipartBody (Mail As String, Msg As Message)
   'find first boundary
   index = Mail.IndexOf2("--" & boundary, index)
   ReadNextLine(Mail)
   Dim headers As StringBuilder
   headers.Initialize
   Do While index < Mail.Length
      Dim line As String
      line = ReadNextLine(Mail)
      If line.Length > 0 Then
         headers.Append(line).Append(" ")
      Else If index < Mail.Length Then
         Dim nextPart As Int
         nextPart = Mail.IndexOf2("--" & boundary, index)
         If nextPart-1 > index Then
            HandlePart(headers.ToString, Mail.SubString2(index, nextPart-1), Msg)
         Else If nextPart = -1 Then
            HandlePart(headers.ToString, Mail.SubString2(index, Mail.Length), Msg)
         End If
         If nextPart = -1 Then Return
         index = nextPart
         ReadNextLine(Mail)
         headers.Initialize
      End If
   Loop
End Sub
 
Upvote 0
Top