Android Question using POP3 and MailParser

sufyan

Member
Licensed User
Longtime User
I get wrong email body when I use the pop3 and mailparser
the pop3 is working fine and the email is check correctly if the mail body is English
but when I use my language is show something else
please check my log
HTML:
LogCat connected to: ec38c6b7
--------- beginning of crash
--------- beginning of system
--------- beginning of main
** Service (pushservice) Start **
true
Count:0
** Service (pushservice) Start **
true
Count:0
** Service (pushservice) Start **
true
Count:0
** Service (pushservice) Start **
true
Count:1
1
Delivered-To: [email protected]
Received: by 10.157.39.55 with SMTP id r52csp1240270ota;
        Tue, 17 May 2016 06:29:58 -0700 (PDT)
X-Received: by 10.28.161.198 with SMTP id k189mr24099595wme.23.1463491798088;
        Tue, 17 May 2016 06:29:58 -0700 (PDT)
Return-Path: <[email protected]>
Received: from mail-wm0-x229.google.com (mail-wm0-x229.google.com. [2a00:1450:400c:c09::229])
        by mx.google.com with ESMTPS id n10si3174072wjy.217.2016.05.17.06.29.57
        for <[email protected]>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 17 May 2016 06:29:58 -0700 (PDT)
Received-SPF: pass (google.com: domain of [email protected] designates 2a00:1450:400c:c09::229 as permitted sender) client-ip=2a00:1450:400c:c09::229;
Authentication-Results: mx.google.com;
       dkim=pass [email protected];
       spf=pass (google.com: domain of [email protected] designates 2a00:1450:400c:c09::229 as permitted sender) [email protected];
       dmarc=pass (p=NONE dis=NONE) header.from=gmail.com
Received: by mail-wm0-x229.google.com with SMTP id g17so27317538wme.1
        for <[email protected]>; Tue, 17 May 2016 06:29:57 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=from:content-transfer-encoding:mime-version:subject:message-id:date
         :to;
        bh=dGiLE9YPzbuTyZKGjhCjpVN8NHV6nJ/OQzZ1uWHak7Y=;
        b=AE6Wh0V0A3Pe3VbrsA0/sO+5OI9MBwrZId0Qx//XzVwuTL/yE4+A3VIiQ/leytS+pj
         WBc2E55BUHWqPwR64LOzw4g0ElU4UoEU3NScLF0Ml6ZyvD5OpetVpPMLMU4jvJXGxHHd
         7Waj28MZlWWP1HrsKdODt856dPDY89a2MwFVFzl5QwILyNSUdPaeenkG82Xx986zWbbT
         qSYTXCy+rRNt98qo9vfpXjYQXVdbpRwUgpf9af8E4hyBSprtbOtb/2XSo/8OjyqRXw4Y
         lbh7SypdaJ3rbtb27BNflCBVURyLHgqiz6livlyTVYsTrM/WT8iFDwDYO7WeS7Cbl7GA
         TE1A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:content-transfer-encoding:mime-version
         :subject:message-id:date:to;
        bh=dGiLE9YPzbuTyZKGjhCjpVN8NHV6nJ/OQzZ1uWHak7Y=;
        b=R8ICPDRQ9NorAPJ2WhyAlNTYBPUPS+9QaxVpjx59abT0b4qJTJjuFRjmJbcvL/bwiG
         yZTr/7C70MtW/8QeUrfNcreHeg0/pWDpI/IuzMr/1TD00GMfbn9LYdfdvrsZmMA/lzuX
         pJyD6oEUyn98knkhi6rJd6RZFfFdHXjeiuXPete017QuppgL2565tAilclMIb7gdLpa+
         Yq6FS8/nd55RtQHBBHTvur7SMIK8AsJDxw98WaiimBe09j9AyOFhoKBDZLzUm5iq8krs
         kE/wI3vr4NKiy5fJB716R90gZLU6A7MJH1bXX/pdpsoVoqzFIi27JAoiUeq5j+nvChiD
         qqQA==
X-Gm-Message-State: AOPr4FUOdVAR+BL9OFbQWR7iM+2xJdKuFvilmUFrKDxb9mL/WrG1V52PK7Z4p8agEBWr7w==
X-Received: by 10.28.56.4 with SMTP id f4mr23125975wma.70.1463491797822;
        Tue, 17 May 2016 06:29:57 -0700 (PDT)
Return-Path: <[email protected]>
Received: from [10.185.247.247] ([37.40.2.227])
        by smtp.gmail.com with ESMTPSA id e8sm3649522wma.2.2016.05.17.06.29.56
        for <[email protected]>
        (version=TLSv1/SSLv3 cipher=OTHER);
        Tue, 17 May 2016 06:29:56 -0700 (PDT)
From: Sufyan <[email protected]>
Content-Type: multipart/alternative;
    boundary=Apple-Mail-91A51E3E-5286-449D-8AEA-EFC8036C9FE6
Content-Transfer-Encoding: 7bit
Mime-Version: 1.0 (1.0)
Subject: Ss
Message-Id: <[email protected]>
Date: Tue, 17 May 2016 17:29:54 +0400
To: [email protected]
X-Mailer: iPhone Mail (13E238)
--Apple-Mail-91A51E3E-5286-449D-8AEA-EFC8036C9FE6
Content-Type: text/plain;
    charset=utf-8
Content-Transfer-Encoding: base64
2LPZgdmK2KfZhiDYp9mE2YXYudmF2LHZiiANCg0KU2VudCBmcm9tIG15IGlQaG9uZQ==
--Apple-Mail-91A51E3E-5286-449D-8AEA-EFC8036C9FE6
Content-Type: text/html;
    charset=utf-8
Content-Transfer-Encoding: quoted-printable
<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=3D=
utf-8"></head><body dir=3D"auto"><div><div style=3D"direction: rtl;">=D8=B3=D9=
=81=D9=8A=D8=A7=D9=86 =D8=A7=D9=84=D9=85=D8=B9=D9=85=D8=B1=D9=8A&nbsp;</div>=<br>Sent from my iPhone</div></body></html>=
Message longer than Log limit (4000). Message was truncated.
file saved: attachment1 (49 bytes)
** Service (pushservice) Start **
true
Count:0
** Service (pushservice) Start **
true
Count:0
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

sufyan

Member
Licensed User
Longtime User
thank you Erel for quick respond
I still have same problem
B4X:
** Service (pushservice) Start **
true
** Activity (main) Pause, UserClosed = false **
Count:0
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (pushservice) Create **
** Service (pushservice) Start **
true
Count:0
** Activity (main) Resume **
** Service (pushservice) Start **
true
Count:1
1
<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=3D=
utf-8"></head><body dir=3D"auto"><div><br><div style=3D"direction: rtl;">=D8=
=B3=D9=81=D9=8A=D8=A7=D9=86</div>Sent from my iPhone</div></body></html>=
 
Upvote 0

sufyan

Member
Licensed User
Longtime User
still I have the same problem , or I m using wrong approach
this is my code
B4X:
Sub POP_DownloadCompleted (Success As Boolean, MessageId As Int, MessageText As String)
 
    If Success = False Then
        Log(LastException.Message)
        Log("ff")
    Else
        Log(MessageId)
        'Parse the mail
        Dim m As Message
        m = MailParser.ParseMail(MessageText, File.DirRootExternal)
        'extract information
        subject=m.Subject
        body=m.Body
        Log(DecodeQuotePrintable(body))
        body=DecodeQuotePrintable(body)
        date=m.ContentType
        'save the data in txt file
        sql2.ExecNonQuery2("INSERT INTO table1 VALUES(?, ?, ?)", Array As Object(subject, body, body))
        Log(body)
       
    End If
End Sub
Sub DecodeQuotePrintable(q As String) As String
   Dim m As Matcher
   m = Regex.Matcher("=\?([^?]*)\?Q\?(.*)\?=$", q)
   If m.Find Then
      Dim charset As String
      Dim data As String
      charset = m.Group(1)
      data = m.Group(2)
      Dim bytes As List
      bytes.Initialize
      Dim i As Int
      Do While i < data.Length
         Dim c As String
         c = data.CharAt(i)
         If c = "_" Then
            bytes.AddAll(" ".GetBytes(charset))
         Else If c = "=" Then
            Dim hex As String
            hex = data.CharAt(i + 1) & data.CharAt(i + 2)
            i = i + 2
            bytes.Add(Bit.ParseInt(hex, 16))
         Else
            bytes.AddAll(c.GetBytes(charset))
         End If
         i = i + 1
      Loop
      Dim b(bytes.Size) As Byte
      For i = 0 To bytes.Size - 1
         b(i) = bytes.Get(i)
      Next
      Return BytesToString(b, 0, b.Length, charset)
   Else
      Return q
   End If
End Sub

and this is my log
B4X:
true
Count:1
1
<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=3D=
utf-8"></head><body dir=3D"auto"><div><br><div style=3D"direction: rtl;">=D8=
=B3=D9=81=D9=8A=D8=A7=D9=86</div>Sent from my iPhone</div></body></html>=
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code:
B4X:
Sub DecodeQuotePrintable(q As String) As String
 
  Dim bytes As List
  bytes.Initialize
  Dim i As Int
  Do While i < q.Length
  Dim c As String
  c = q.CharAt(i)
  If c = "_" Then
  bytes.AddAll(" ".GetBytes("utf8"))
  Else If c = "=" And i < q.Length - 1 Then
  Dim hex As String
  hex = q.CharAt(i + 1) & q.CharAt(i + 2)
  i = i + 2
       Try
         bytes.Add(Bit.ParseInt(hex, 16))
       Catch
          bytes.AddAll(hex.GetBytes("utf8"))
       End Try
   
  Else
  bytes.AddAll(c.GetBytes("utf8"))
  End If
  i = i + 1
  Loop
  Dim b(bytes.Size) As Byte
  For i = 0 To bytes.Size - 1
  b(i) = bytes.Get(i)
  Next
  Return BytesToString(b, 0, b.Length, "utf8")
End Sub
 
Upvote 0

sufyan

Member
Licensed User
Longtime User
Eral is not fully working ,srill the mail is in html (I guess)

B4X:
** Service (pushservice) Start **
true
Count:0
** Activity (main) Resume **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (pushservice) Create **
** Service (pushservice) Start **
true
Count:0
** Service (pushservice) Start **
true
Count:0
** Service (pushservice) Start **
true
Count:0
** Service (pushservice) Start **
true
** Activity (main) Pause, UserClosed = false **
Count:0
** Service (pushservice) Start **
true
Count:0
** Service (pushservice) Start **
true
** Activity (main) Resume **
Count:1
1
<html><head><meta http-equiv"content-type" content"text/html; charset
utf-8"></head><body dir"auto"><div style"direction: rtl;">سف
يان</div></body></html>
 
Upvote 0
Top