[Gambas-user] Problems reading pdf attachment using pop3 client

Benoît Minisini gambas at ...1...
Sun Dec 8 10:30:09 CET 2013


Le 30/11/2013 13:14, bill-lancaster a écrit :
> Some further information.
>
> If I download emails containing pdf files using .Message.ToString() and save
> the email to file then try to decode the attachment with-:
>
> Dim hMimeMessage As MimeMessage
> Dim hMimePart As MimePart
> Dim sTemp As String
>       Dialog.Title = "Select a text file"
>       If Dialog.OpenFile() Then Return
>       sTemp = File.Load(Dialog.Path)
>       hMimeMessage = New MimeMessage(sTemp)
>       For Each hMimePart In hMimeMessage.Part
>            If hMimePart.Disposition = "attachment" Then
>                 File.Save(User.Home &/ "New Folder" &/ hMimePart.FileName,
> Mime.Decode(sTemp, hMimePart.ContentEncoding))
>            Endif
>       Next
>
> The saved file is unreadable.
> kmail however can open the saved email and displays the attachment OK.
>
> BTW I'm using Gambas 3.5
>

You are not doing the right thing: Mime.Decode() is just a decoding 
routine (like UnBase64$). You don't have to use it, all is done 
automatically by the "Data" property of the message part.

Moreover, a MIME message is a recursive structure. Usually mail clients 
hides that by displaying the message contents linearly. So you must 
parse the parts recursively until you find your attachment.

Here is an example:

--8<--------------------------------------------------------------------

' Gambas module

Private $iLevel As Integer

Public Sub Main()

   Dim hMessage As New MimeMessage

   ' MyPopMessageData is retrieved (for example) with the
   ' Pop3Client[].Text property

   hMessage = New MimeMessage(File.Load(MyPopMessageData))

   PrintPart(hMessage.Part)

End

Public Sub PrintPart(hPart As MimePart)

   Dim hChild As MimePart

   Print String$($iLevel, "| "); "| "
   Print String$($iLevel, "| "); "+-"; If(hPart.Count, "+", "-");;
   Print hPart;; hPart.ContentType;; hPart.FileName

   Inc $iLevel
   For Each hChild In hPart
     PrintPart(hChild)
   Next
   Dec $iLevel

   If hPart.ContentType = "application/pdf" Then
     ' I found the PDF attachment!
     File.Save("~/found-it.pdf", hPart.Data)
   Endif

End

--8<--------------------------------------------------------------------

Regards,

-- 
Benoît Minisini




More information about the User mailing list