Android Question Dealing with Apple Mail and multiple base64 images

William Hunter

Active Member
Licensed User
Longtime User
I have been working to add functionality to the Mail Parser. There is one mail type that I cannot figure out how to deal with. The attached txt file is for a piece of Apple Mail. It contains a few lines of text and four base64 images. I am aware of Peter Simpson’s Base64 encode/decode image library. But, have no idea how to process this mail in preparation for decoding.

I would like to create a Sub that will, in principle, look something like the rough illustration below. I don’t know how to to create this Sub, and I hope that someone who has done this will share their knowledge.
B4X:
Mail = DecodeAppleMail(Mail)
Msg.Body = Mail

Sub DecodeAppleMail(MyString As String) As String
_ _ _ _ _ _

Return MyString
End Sub
EDIT: I have tried to upload the file containing the four images, but it is too large. Instead, I have uploaded a similar file containing only one image. It is dealing with images, whether singular or multiple, that confounds me. I just don't know where to begin.

Any help is greatly appreciated

Regards
 

Attachments

  • RawMail2.txt
    174.2 KB · Views: 323

William Hunter

Active Member
Licensed User
Longtime User
You can use StringUtils to convert Base64 string to bytes. Converting the bytes to image is very simple: https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/#content

Were you able to extract the base 64 string from the message?
Thank you for your reply Erel. No, I don't know how to extract a base64 string. It becomes a greater mystery to me, when there are a number of images in the message. I don't know how to use the boundaries to separate each text message and image for conversion to a string. This is one situation where I'm lost. I just don't know where to start. If you, or another member, could post a complete example of how to convert this type of message to Msg.Body, I'm sure it would help more than me.

Regards
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Thank you Erel. I have run the same code and RawMail2.txt with the original MailParser. Yes it does parse correctly. But, when parsing the same mail type with more than one image I get the following error.
B4X:
** Activity (main) Create, isFirst = true **
file saved: attachment1 (651139 bytes)
file saved: attachment2 (927423 bytes)
file saved: attachment3 (680445 bytes)
file saved: attachment4 (576882 bytes)
file saved: attachment5 (532326 bytes)
file saved: attachment6 (569336 bytes)
AppleMail Processing
main_pop_downloadcompleted (java line: 377)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:523)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(CanvasWrapper.java:501)
    at anywheresoftware.b4a.keywords.Common.LoadBitmap(Common.java:1308)
    at anywheresoftware.b4a.samples.pop3.main._pop_downloadcompleted(main.java:377)
    at anywheresoftware.b4a.samples.pop3.main._activity_create(main.java:333)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.samples.pop3.main.afterFirstLayout(main.java:102)
    at anywheresoftware.b4a.samples.pop3.main.access$000(main.java:17)
    at anywheresoftware.b4a.samples.pop3.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
java.lang.RuntimeException: Error loading bitmap.
** Activity (main) Resume **
In any event using Activity.SerBackgroundImage isn't going to accomplish what I need to do. I need to display Msg.Body in a WebView having created MyBody in parsing the mail. I need to end up with Msg.Body = MyBody. Mail of this type can contain a number of images, each of which could be preceded by a line of descriptive text. A means of pulling all of this together for display in a WebView so far eludes me.

Regards
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
This was just an example. You can do whatever you like with the bitmap.
Thank you Erel. I think I have been wanting to do something that is not possible. I have created an app that is built around the MailParser. I load M.Body into an array using the code below. As you can see M.Body must be a string.
B4X:
Dim M AsMessage
M = MailParser.ParseMail(MessageText, File.DirRootExternal & "/MPtemp")
arraySubject(MsgIndex) = M.Subject
arrayFrom(MsgIndex) = M.fromfield
arrayBody(MsgIndex) = M.Body
Later in my code M.Body is retrieved from the array, and displayed in a WebView. So far, with the exception of AppleMail, I can parse the body of mail recieved, whether it be html, quotedprintable or plain text etcetera, into a string. Not being interested in exposing clickable attachments to the user, this has been fairly simple.

My app is not intended to be a full blown email client. It is used to review mail on the mail server, prior to downloading with an email client. The headers are displayed in a ScrollView. The user can select those messages they wish to delete from the server, by checking an associated CheckBox in the Subject (left) column. A long press on any item in the From (right) column will display the message body for that item in a WebView, for assessment by the user.

While I would prefer to display the entire message body, text and images, if this is not possible in this instance, I would like to extract the text into a string and ignore the images. I have tried to attach a file that contains four images, each of which is preceded by a line of text, unfortunately the file is too large to upload. Except that it contains four images instead of one, it is in the same format as the file uploaded in Post #1 above. How can I ignore the images and extract only the text from such a file?

I have attached two image files showing the interface of my app. I’ve been trying to keep it both simple and intuitive, and in doing so may have created an issue I can’t overcome. Help is greatly appreciated.

EDIT: Parsing as MultiPart extracts the text portion, and answers my last question. I still would like to display the entire message body, including images, but I can't find a way of doing that.
B4X:
multipart = True
If FindBoundary(line) = False Then
     line = ReadNextLine(Mail)
     FindBoundary(line)
End If

Regards
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…