Android Question Decoding of UTF-8 email message body from POP3?

b4auser1

Well-Known Member
Licensed User
Longtime User
How to decode UTF-8 email message body taken using POP3? If all the characters in the body ASCII ones, then there is no an issue, but for 2 and more byte coded characters for non-ascii (f.e. non-english characters) it is necessary to convert from UTF-8 to usual unicode characters, which are used in Java (B4A, B4J)
 

b4auser1

Well-Known Member
Licensed User
Longtime User
I send and receive using jNet classes and parse message using MailParser. Please find log below.
I send text in ansi win 1251 cyrillic using smtp from jNet to gmail account.
I see text body correctly in Gmail web client. I attached original of message, taken from Gmail. It has Content-Type: text/plain; charset="utf-8". Which is why I can suggest, that text in body converts to not readable when POP class from jNet downloads it from Gmail via POP3.
 

Attachments

  • log.txt
    747 bytes · Views: 297
  • orginal from google.txt
    846 bytes · Views: 307
Upvote 0

b4auser1

Well-Known Member
Licensed User
Longtime User
I don't read the file. I just use this code
B4X:
Private m_smtp As SMTP
...

    m_smtp.Subject = "Тема"

    m_smtp.Body = "Text message. Текстовое сообщение"

    m_smtp.HtmlBody = False
   
    m_smtp.Send
...
 
Upvote 0

Eldritch

Member
Licensed User
Longtime User
HI

I'm having the same problem with the subject field even though I have used you "fix".

Se image where the subject is no decoded.


Here is my code.

Sub POP_DownloadCompleted (Success AsBoolean, MessageId AsInt, MessageAsString)

Dim From AsString
Dim b() AsByte = Message.GetBytes("ISO-8859-1")

Message=BytesToString(b, 0, b.Length, "UTF-8")

If Success Then

Dim m AsMessage
m = MailParser.ParseMail(Message, File.DirRootExternal)

If sf.InString(m.FromField,">")>0 Then
From=m.Subject
From=m.FromField.SubString(m.FromField.IndexOf("<")+1)
From=From.SubString2(0,From.IndexOf(">"))
Else
From=m.FromField
EndIf
From=From.Trim
EndIf
111111111111111
End Sub
 

Attachments

  • mailparser.png
    mailparser.png
    9.7 KB · Views: 330
Upvote 0

Eldritch

Member
Licensed User
Longtime User
Attached the log(message)

The emails from flysas.com and apoteker.dk both
 

Attachments

  • messagelog.txt
    15.1 KB · Views: 376
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is a different issue than the one discussed in this thread. The subject is encoded as base 64 string. You will need to modify MailParser in order to handle this string.

The current implementation takes the string as is and puts it in the Subject field:
B4X:
Msg.Subject = line.SubString(line.IndexOf(":") + 1)

You will need to change it to this (untested) code:
B4X:
Dim su As StringUtils
Dim bytes() As Byte = su.DecodeBase64(line.SubString(line.IndexOf("B?") + 2))
Msg.Subject = BytesToString(bytes, 0, bytes.Length, "utf8")
 
Upvote 0

Eldritch

Member
Licensed User
Longtime User
I then get this

** Activity (main) Create, isFirst = true **

(Main, 51) FullScreen or IncludeTitle properties in layout file do not match the activity attributes settings. (warning #1004)
** Activity (main) Resume **

java.io.IOException: Bad Base64 input character decimal 58 in array position 6

at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1204)
at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1259)
at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1227)
at anywheresoftware.b4a.objects.StringUtils.DecodeBase64(StringUtils.java:51)
at com.ar.SpamCleaner.mailparser._parseheaders(mailparser.java:234)
at com.ar.SpamCleaner.mailparser._parsemail(mailparser.java:315)
at com.ar.SpamCleaner.main._pop_downloadcompleted(main.java:771)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.BA$3.run(BA.java:332)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)

at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

It is not all emails where the UTF-8 not converted, just some.
 
Upvote 0

Eldritch

Member
Licensed User
Longtime User
Hi Erel

Got it working by your advices. I ended up with this subject part

Case "subject"
tmpString=line.ToLowerCase
If tmpString.Contains("utf-8?b?") Then
Dim su AsStringUtils
Dim bytes() AsByte = su.DecodeBase64(line.SubString(line.IndexOf("B?") + 2))
Msg.Subject = BytesToString(bytes, 0, bytes.Length, "utf8")
Else
Msg.Subject = line.SubString(line.IndexOf(":") + 1)
EndIf

Thanks Erel
You are always a great help.
 
Upvote 0
Top