B4J Question Email prase Subject Line issue

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the email based server example to check for new emails from my server. - https://www.b4x.com/android/forum/threads/building-a-mini-email-based-server.35030/#content

It downloads the email fine as I can see 1 new email download. (I made no changes to the example, except for my email server settings)

When it downloads the message, I can read who the email is from and can also read the Body of the email (which seems to be in HTML, which is OK).

However the subject line looks encrypted like shown below..

subject: =?utf-8?B?TmVzcyAvIENBU........aXRoIFNJTSAvIDA0NzYzNDkxNjQ=?=
(removed some of it while I post it on the forum)

When viewing my email in the web browser (in roundcube) it shows the subject line fine when viewing it in the web page.

When I view the raw email text in roundcube (the email headers) it shows the same encrypted text, except it displays the text fine on the page when viewing the subject line.

In the roundcube email headers it shows the from email, and subject line like:

To: =?utf-8?B?T........dGlvbg==?= <[email protected]>
Subject: =?utf-8?B?TmVzcyAvIENBU........aXRoIFNJTSAvIDA0NzYzNDkxNjQ=?=

(I removed the email while I post it on the forum)

But shows it fine when viewing it in the normal GUI. Seems that they somehow decrypt it?

Any ideas on how to read it with B4J ?

Currently when the customer emails my work Gmail account, I am then making it forward to my test email account which isn't on Gmail. I then am making my B4J app connect to my non Gmail account and trying to read the email. (not sure if this is causing this issue? Wouldn't think so?)

I seem to be able to read all the other values fine (Mail from, Body etc, except the subject line which I need)
 

aaronk

Well-Known Member
Licensed User
Longtime User
Got it to work.

Seems that it was sent as base64. Doing the following allowed me to view the subject line and similar thing can be used to receive the 'To' field as well since this is also in base64.

B4X:
Dim subject As String
        subject = m.Subject.SubString(11)
        subject = subject.SubString2(0,subject.Length - 2)
        Log(Base64Decode(subject))


Sub Base64Decode(MyString As String) As String
    If Regex.Matcher2("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$", _
     Regex.CASE_INSENSITIVE, MyString).Find = True Then
        'Log("Base64Text")
        Try
            Dim su As StringUtils
            Dim Data() As Byte = su.DecodeBase64(MyString)
            MyString = BytesToString(Data, 0, Data.Length, "UTF8")
        Catch
            Log("LastException: " & LastException)
        End Try
    End If
    Return MyString
End Sub
 
Upvote 0
Top