Android Question MailParser and filename of BASE64 attachment

peacemaker

Expert
Licensed User
Longtime User
Why always names are "attachmentXX" ? In the body filenames are simple ANSI text - why it's not decoded well ?
B4X:
--refeiks-5ea0de8b-0b98-4b01-8d5a-8d9243b435f0
Content-Type: application/zip
Content-Disposition: attachment; filename=777.zip
Content-Transfer-Encoding: base64

UEsDBBQAAAAIACS7dUgC+boFmQwAANQ2AAAMAAAAcm91dGUwMDQuZGF0xdv5c9T1HcfxT3Y3NyHX
kgvIRe4QcocjHCEQEkAOBQS5AoRAAuQgB4KlVbl0RuTQarXaIgqieFTx7tTqdNIp/c0/QK0z7S+d
6XRsp2X6Qyt9vZes7D6JiMWZhtnZPB/5fr/7/X6+n/1mswkpia9kx7tJvipfk69NtwW+Zl+uq9K/
anewMsIle7+Ifm4gzedzzmVo2ZEY.................
 

peacemaker

Expert
Licensed User
Longtime User
Now it's OK for me

B4X:
Sub HandlePart(Headers As String, Body As String, Msg As Message)
    If Regex.Matcher2("Content-Transfer-Encoding:\s*base64", _
        Regex.CASE_INSENSITIVE, Headers).Find Then
        'we are dealing with an attachment
        Dim filename As String
        Dim m As Matcher
        m = Regex.Matcher2("filename=\s*q([^q]+)q".Replace("q", QUOTE), Regex.CASE_INSENSITIVE, Headers)
        If m.Find Then
            filename = m.Group(1)
        Else
            filename = "attachment" & (Msg.Attachments.Size + 1) & DateTime.Now
        End If
        If filename.StartsWith("attachment") Then
            m = Regex.Matcher2("filename=\s*([^\s]+)\s", Regex.CASE_INSENSITIVE, Headers)
            If m.Find Then
                filename = m.Group(1)
            Else
                filename = "attachment" & (Msg.Attachments.Size + 1) & DateTime.Now
            End If
        End If
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
But ZIP-archive is saved corrupted :(
Latest 3 bytes is not zero. "rebilt" file is repaired by WinRAR
Why ?

TempDownload.png
 
Upvote 0
Top