Android Question Extracting and decoding Base64 eMail Body

William Hunter

Active Member
Licensed User
Longtime User
I have attached a raw email message encoded Base64. I would like to extract the message body, and then decode it to utf-8. There don't seem to be any tags to identify the beginning and end of the body, so I'm at a loss as to how to do this. The code below is for a Sub I have created to decode Base64, but it does not deal with extracting the body. Is there a way of doing this? Any help gretly appreciated.

Best regards
B4X:
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
 

Attachments

  • RawMailLibrary.txt
    8.1 KB · Views: 326

DonManfred

Expert
Licensed User
Longtime User
. There don't seem to be any tags to identify the beginning and end of the body
The message has the following boundary.

----boundary_223389_fb6d2117-94ea-4f10-8100-eaf0eda49579

This is the splitpoint of the parts of the mail.
The first is the textpart, the second the html-part


B4X:
----boundary_223389_fb6d2117-94ea-4f10-8100-eaf0eda49579
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

RGVhciBDYWxnYXJ5IFB1YmxpYyBMaWJyYXJ5IHVzZXIsDQoNClRoZSBmb2xsb3dpbmcg
ZGlnaXRhbCB0aXRsZSB0aGF0IHlvdSBwbGFjZWQgb24gaG9sZCBoYXMgYmVlbiBhdXRv
bWF0aWNhbGx5IGNoZWNrZWQgb3V0IHRvIHlvdToNCg0KICAgQ29uY2VhbGVkIGluIERl
YXRoOiBJbiBEZWF0aCBTZXJpZXMsIEJvb2sgMzgNCiAgIGJ5IEouIEQuIFJvYmINCiAg
IGVCb29rIA0KDQpTaWduIGludG8gdGhlIDxDYWxnYXJ5IFB1YmxpYyBMaWJyYXJ5PiBk
aWdpdGFsIGNvbGxlY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WRkaW5nOjA7Ij5TaWduIGludG8g
dGhlIDxhIHN0eWxlPSJjb2xvcjojMmJhNmNiO3RleHQtZGVjb3JhdGlvbjpub25lIiBo
cmVmPSJodHRwOi8vY2FsZ2FyeS5saWIub3ZlcmRyaXZlLmNvbS9CQU5HQXV0aGVudGlj
YXRlLmRsbD9BY3Rpb249QXV0aENoZWNrJmFtcDtVUkw9TXlBY2NvdW50Lmh0bT9QZXJQ
YWdlPTQwIj5DYWxnYXJ5IFB1YmxpYyBMaWJyYXJ5IGRpZ2l0YWwgY29sbGVjdGlvbjwv
YT4gdG8gYWNjZXNzIHRoaXMgdGl0bGUgZnJvbSB5b3VyIENoZWNrb3V0cyBwYWdlLjwv
cD48L3RkPjwvdHI+DQoJCQkJCTx0cj48dGQgc3R5bGU9ImxpbmUtaGVpZ2h0OjA7Zm9u
dC1zaXplOjA7bWFyZ2luOjA7cGFkZGluZzowOyIgaGVpZ2h0PSIxMiI+Jm5ic3A7PC90
ZD48L3RyPg0KPHRyPjx0ZCB2YWxpZ249InRvcCIgYWxpZ249ImxlZnQiIHN0eWxlPSJj
b2xvcjojMjIyMjIyO2Rpc3BsYXk6YmxvY2s7Zm9udC1mYW1pbHk6J0hlbHZldGljYScs
J0FyaWFsJyxzYW5zLXNlcmlmO2ZvbnQtd2VpZ2h0Om5vcm1hbDt0ZXh0LWFsaWduOmxl
ZnQ7bGluZS1oZWlnaHQ6MS4zO3dvcmQtYnJlYWs6bm9ybWFsO2ZvbnQtc2l6ZToxNHB4
O21hcmdpbjowO3BhZGRpbmc6MDsiPlRoYW5rIHlvdSE8L3RkPjwvdHI+DQoJCQkJPC90
YWJsZT4NCgkJCTwvdGQ+DQoJCTwvdHI+DQoJPC90YWJsZT4NCjwvYm9keT4NCjwvaHRt
bD4=
----boundary_223389_fb6d2117-94ea-4f10-8100-eaf0eda49579--
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Thank you DonManfred. Yes, I know the boundaries are there, but the MailParser doesn't recognize them. When parsed this message comes through as a 0 byte file. In an earlier post Erel has stated that the MailParser is specifically not compatible with multipart/alternative, so the result is not unexpected. I am trying to find a way to display this type of message in a WebView by extracting the text part for the body by some other means, and then decoding it. I haven't even thought of trying to decode the html. Do you have any other thoughts?

Best regards
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've just tested MailParser with the mail you posted and it parses this email successfully.

It treats each part as an attachment:
B4X:
Dim m As Message = MailParser.ParseMail(File.ReadString(File.DirAssets, "RawMailLibrary.txt"), File.DirInternal)
For Each s As String In m.Attachments
   Log(File.ReadString(File.DirInternal, s))
Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…