Android Question Reading attached file using POP3

prokli

Active Member
Licensed User
Longtime User
POP_DownloadCompleted:
 POP_DownloadCompleted (Success As Boolean, MessageId As Int, MessageText As String)
    '--------------------------------------------
    ' Fired when pop.DownloadMessage is finished
    '--------------------------------------------
    Dim emsg As Message
    Try
    If Success = False Then
        WriteLog("Failed in POP_DownloadCompleted due to " & LastException.Message)
    Else
        emsg = MailParser.ParseMail(MessageText, File.DirDefaultExternal & "/" & Misc.usrfolder)
        
        Dim attfile As String
        attfile = emsg.Attachments.Get(0)     ' ** Code fails at this point!!!!
         'do anything with this attachment.................

    End If
    pop.Close
    Catch
        WriteLog("POP_DownloadCompleted failed " & LastException.Message)
        pop.Close
    End Try
End Sub

I just want to save an attached file to a certain folder using the POP library. Refer to line "**" where code fails.
In Outlook I have prepared a small email and attachhed a simple textfile to it.
While debugging the brakpoint at ** tells me, that there is no attachment at all.
 

drgottjr

Expert
Licensed User
Longtime User
without seeing a little more of your code, i would say i have a problem here:
B4X:
        emsg = MailParser.ParseMail(MessageText, File.DirDefaultExternal & "/" & Misc.usrfolder)

and here:
B4X:
        Dim attfile As String
        attfile = emsg.Attachments.Get(0)     ' ** Code fails at this point!!!!
         'do anything with this attachment.................

(also, the try/catch block is backwards, but leave that aside for the moment.)

in the first instance, are you sure File.DirDefaultExternal & "/" & Misc.usrfolder is working?

in the second, you can't assume get() is going to work if you don't test for attachments.size first. that part is a coding error. the first part is not a coding error, but it may violate android permissions. that's why i ask if your use of file.dirdefaultexternal is working as expected.

i use pop3 with the mailparser class. they're both pretty reliable.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
in the first instance, are you sure File.DirDefaultExternal & "/" & Misc.usrfolder is working?
Code that uses DirDefaultExternal is considered as broken code.
I guess the answer here is NO

 
Upvote 0

prokli

Active Member
Licensed User
Longtime User
This code works fine for me:
Getting email message:
emsg = MailParser.ParseMail(MessageText, File.DirDefaultExternal & "/" & Misc.usrfolder)

Whenever I send a email to my android device keeping an attachment (jpg, pdf...) the file is propperly store to
File.DirDefaultExternal/mydata. So File.DirDefaultExternal seems to work on my device.

Furthermre I checked the number of attachments according to this code:

B4X:
If emmsg.Attachments.Size > 0 Then
    'attfile = emmsg.Attachments.Get(0)
    'File.Copy(File.DirDefaultExternal & "/" & Misc.usrfolder, "medikamenten_plan.csv", File.DirDefaultExternal, "medikamenten_plan.csv")
End If

And size is always 0 even when the email keeps an attachment.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
1) please show the full MessageText
2) please show the folder listing for File.DirDefaultExternal & "/" & Misc.usrfolder

thanks.
 
Upvote 0

prokli

Active Member
Licensed User
Longtime User
Erel, that is exactly what I did:

B4X:
rp.GetSafeDirDefaultExternal(Misc.userfolder)

Once again: the problem does not occur when storing to this location at all. Permission seems to be set in teh right way
But: some email attachments are store to "Misc.userfolder", some are not. For example: JPG and PDF Files are stored, but Txt and XLS files are not stored.
Originally I was thinking that certain attchments have been deleted by my provider or Virus scanner.
This is not the fact!
In meantime I found a location inside your Email parser (MailParser module) which seems to refuse such kind of file attachments:

B4X:
Public Sub HandlePart(Headers As String, Body As String, Msg As Message)
    Try
    If Regex.Matcher2("Content-Transfer-Encoding:\s*base64", _
        Regex.CASE_INSENSITIVE, Headers).Find Then
    ............................
    ............................
    
        Dim su As StringUtils
        Dim out As OutputStream
        'save attachment right here
        out = File.OpenOutput(dir, filename, False)
        Dim data() As Byte
        data = su.DecodeBase64(Body)
        Log("file saved: "  & filename & " (" & data.Length & " bytes)")
        out.WriteBytes(data, 0, data.Length)
        out.Close
        Msg.Attachments.Add(filename)
    Else If Regex.Matcher2("Content-Type:\s*text/", _
        Regex.CASE_INSENSITIVE, Headers).Find Then
        Msg.Body = Body
    End If

Several attachment types are handled inside the "Esle If" construct what means that these files are not stored
 
Upvote 0

prokli

Active Member
Licensed User
Longtime User
Yes, this is what I did.
I just changed this part slightly:

B4X:
Dim data() As Byte
data = su.DecodeBase64(Body)
Thanks!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…