Android Question MailParser / POP3 Problem handling attachments sent via SMTP

MFfromGermany

Member
Licensed User
Hey guys,

I've written a windows-forms-application in VB. This application sends mails via SMTP and I receive them with the android app I've written in B4A. So far it works.

But when I add an attachment (test.txt for example) to the mail in VB, the B4A MailParser does not seem to recognize this attachment. However, when I send an email via Gmail directly from their homepage, MailParser recognizes the txt-file and I can read from it.

I've read about this problem before but couldn't find any solution for it. Maybe you guys have an idea how to solve that.

Best regards
 

MFfromGermany

Member
Licensed User
Well, I guess the raw message is "MessageText" in POP_DownloadCompleted Sub. If so, then here you got:

B4X:
file saved: attachment1 (3 bytes)
Delivered-To: rm2314fzg01@gmail.com
Received: by 10.36.91.200 with SMTP id g191csp369010itb;
        Thu, 10 Dec 2015 01:24:52 -0800 (PST)
X-Received: by 10.194.118.74 with SMTP id kk10mr11274275wjb.5.1449739492867;
        Thu, 10 Dec 2015 01:24:52 -0800 (PST)
Return-Path: <rm2309nfp01@gmail.com>
Received: from mail-wm0-x243.google.com (mail-wm0-x243.google.com. [2a00:1450:400c:c09::243])
        by mx.google.com with ESMTPS id s204si16488891wmf.121.2015.12.10.01.24.52
        for <rm2314fzg01@gmail.com>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Thu, 10 Dec 2015 01:24:52 -0800 (PST)
Received-SPF: pass (google.com: domain of rm2309nfp01@gmail.com designates 2a00:1450:400c:c09::243 as permitted sender) client-ip=2a00:1450:400c:c09::243;
Authentication-Results: mx.google.com;
       spf=pass (google.com: domain of rm2309nfp01@gmail.com designates 2a00:1450:400c:c09::243 as permitted sender) smtp.mailfrom=rm2309nfp01@gmail.com;
       dkim=pass header.i=@gmail.com;
       dmarc=pass (p=NONE dis=NONE) header.from=gmail.com
Received: by mail-wm0-x243.google.com with SMTP id c201so2118784wme.3
        for <rm2314fzg01@gmail.com>; Thu, 10 Dec 2015 01:24:52 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=message-id:date:mime-version:from:to:subject:content-type;
        bh=hIt5VF0Dl2dqHtnKzEqXKjT5LjhPMfMgfDkP9ZL4Jgk=;
        b=oNBgAgDO1ICHmLRvt8m4knF4JoXZT5ctJUBJPVxqG+x9oOu9Yu9KhA8xK/NtB8zIKg
         Z06uC3x9wN3HLhk4osOxkDbBIv8/6/IgT2d+jdzUzJIXwAv6U+jxqzP6T1MFY5H46AEM
         uI52o6IV/Z+Lb6O6kbn8p4vDRaZQcAunkxvR+70EpQEWllziqm5SNqFHqBtMIhhk+S+5
         Gi7o05FlvdYL3zK6jAr/k7K3PuHYnbB0nusOFxz04fVWKPSCg4bz34T/HKYv9z1B5Zj8
         eRghfhPSqhtpySMu6j8Y80jtP4+DN+kTw0BXw7q7EWRI4GnqGxilhf7jKMvaw+a8CrOy
         BqBA==
X-Received: by 10.194.79.37 with SMTP id g5mr11378653wjx.107.1449739492659;
        Thu, 10 Dec 2015 01:24:52 -0800 (PST)
Return-Path: <rm2309nfp01@gmail.com>
Received: from Desktop-PC ([2a02:8070:6182:5300:49a:2653:b6b4:f349])
        by smtp.gmail.com with ESMTPSA id ft4sm11406825wjb.37.2015.12.10.01.24.51
        for <rm2314fzg01@gmail.com>
        (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128);
        Thu, 10 Dec 2015 01:24:51 -0800 (PST)
Message-ID: <566944e3.2467c20a.2e612.ffffd022@mx.google.com>
Date: Thu, 10 Dec 2015 01:24:51 -0800 (PST)
X-Google-Original-Date: 10 Dec 2015 10:24:52 +0100
MIME-Version: 1.0
From: "rm2309nfp01@gmail.com" <rm2309nfp01@gmail.com>
To: rm2314fzg01@gmail.com
Subject: Test
Content-Type: multipart/mixed;
boundary=--boundary_2_6e2820fd-15b6-413f-98ac-7deafba0a178
----boundary_2_6e2820fd-15b6-413f-98ac-7deafba0a178
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Test
----boundary_2_6e2820fd-15b6-413f-98ac-7deafba0a178
Content-Type: application/octet-stream; name=test.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment
VGVzdA0K
----boundary_2_6e2820fd-15b6-413f-98ac-7deafba0a178--

This seems to me that the attachment is both recognized and saved, but how come I can neither read from it nor find it on my smartphone?
 
Upvote 0

MFfromGermany

Member
Licensed User
Here you got the raw message in an txt-File.

Meanwhile I tried changing the directory the attachment should be saved to - problem's still remaining.

By the way, when I send an email trough B4J it doesn't work neither.

Thank you for your help so far.
 

Attachments

  • MsgTxt.txt
    3 KB · Views: 380
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change ParseMultipartBody 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 Then nextPart = Mail.Length + 2
       If nextPart-2 > index Then
         HandlePart(headers.ToString, Mail.SubString2(index, nextPart-2), Msg)
       End If
       index = nextPart
       ReadNextLine(Mail)
       headers.Initialize
     End If
   Loop
End Sub

It will return the attachment as attachment1. You can try it with this code:
B4X:
Dim m As Message = MailParser.ParseMail(File.ReadString(File.DirAssets, "MsgTxt.txt"), File.DirRootExternal)
Log($"Number of attachments ${m.Attachments.Size}"$)
Log($"Attachment 1: ${File.ReadString(File.DirRootExternal, m.Attachments.Get(0))}"$)
 
Upvote 0
Top