Android Question HTML emal body shows poorly in WebView

William Hunter

Active Member
Licensed User
Longtime User
I am loading an email msg body into a WebView as HTML. Some messages display correctly while others do not. They are all either HTMLor XHTML.

I have attached three image files for a message that does not show correctly. The first is how the message displays in the WebView. The second shows the first few lines of HTML code for the page. The third shows how the message displays in my email client. I don’t have a great understanding of HTML, XML and XHTML. Would someone please take a look at these images and tell me whether this is a case of a limitation in WebView, or otherwise? I don’t think it could be bad HTML, otherwise my email client would have a problem with it as well. Any help greatly appreciated.


Best regards
 

William Hunter

Active Member
Licensed User
Longtime User
Can you post the text of one of the messages that doesn't loop properly?
Thank you for your reply. This is the text as logged.
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
file saved: attachment1 (332 bytes)
file saved: attachment2 (350 bytes)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
=20=20=20=20
<html>
=20=20=20=20
  <head>
=20=20=20=20
  <title>Hudson's Bay</title>
  <!-- this STYLE needs to be placed in the header template and the links=
 to the fonts updated to reflect their actual location-->
<style type=3D"text/css">
@font-face {
   font-family: 'kings_caslon_typoregular';
   src: url('fonts/kingscaslon_typo_rg-webfont.eot');
   src: url('fonts/kingscaslon_typo_rg-webfont.eot?#iefix') format('embedded-=
opentype'), url('fonts/kingscaslon_typo_rg-webfont.woff2') format('woff2'),=
 url('fonts/kingscaslon_typo_rg-webfont.woff') format('woff'), url('fonts/k=
ingscaslon_typo_rg-webfont.ttf') format('truetype'), url('fonts/kingscaslon=
_typo_rg-webfont.svg#kings_caslon_typoregular') format('svg');
   font-weight: normal;
   font-style: normal;
}
</style>
  </head>
=20=20=20=20
<body><img src=3D"http://enews.thebay.com/a/hBXoKdsB8H8DrB9QZNENsh1Se4N/spa=
cer.gif" width=3D"1" height=3D"1" style=3D"display:none !important; max-hei=
ght:1px !important; max-width:1px !important; border:0 !important; backgrou=
nd-color:transparent !important; padding:0 !important; outline:0 !important=
; min-height:1px !important; min-width:1px !important; height:1px !importan=
t; width:1px !important;" alt=3D"">
=09
   <table width=3D"100%" border=3D"0" cellspacing=3D"0" cellpadding=3D"0">
     <tr>
     <td valign=3D"top" align=3D"center" bgcolor=3D"#ffffff">
<!--------------------------------PREHEADER START--------------------------=
------>
<table width=3D"640" border=3D"0" align=3D"center" cellpadding=3D"0" cellsp=
acing=3D"0" id=3D"PreHeaderEN">
  <tr>
  <td align=3D"left" style=3D"font-family:'Times New Roman', Times, serif=
; font-weight:normal; color:#000000; font-size:14px; padding-bottom:4px; bo=
rder-bottom:1px solid #dddddd;"><a href=3D"http://enews.thebay.com/a/hBXoKd=
sB8H8DrB9QZNENsh1Se4N/phtext?email=3Dhunter003@telus.net&email=3Dhunter003@=
telus.net&REF_ID=3DEMLHB62719DAILY" style=3D"color:#000000; font-size:14px;=
 line-height:14px; text-decoration:none; border-bottom:0px; border-bottom-c=
olor:#6d6d6d; border-bottom-style:solid;">Shop Beauty</a></td>
  </tr>
</table>
<!--------------------------------PREHEADER END----------------------------=
---->=20
<!--------------------------------HEADER TEXT LINK OPTIONS START ----------=
---------------------->
<table width=3D"640" border=3D"0" align=3D"center" cellpadding=3D"0" cellsp=
acing=3D"0" id=3D"HeaderTextLinksEN">
  <tr>
  <td><table width=3D"100%" border=3D"0" cellpadding=3D"0" cellspacing=3D=
"0">
  <tr>
  <td height=3D"22" align=3D"left" valign=3D"middle"><a style=3D"fo=
nt-family:'Times New Roman', Times, serif; font-weight:normal;color:#000000=
;font-size:12px;padding-bottom:4px;" href=3D"http://enews.thebay.com/a/hBXo=
KdsB8H8DrB9QZNENsh1Se4N/viewonline?t_params=3DEMAIL%3Dhunter003%2540telus.n=
et%26REF_ID%3DEMLHB62719DAILY">View in a web browser</a></td>
  <td align=3D"right" valign=3D"middle"><a style=3D"font-family:'Ti=
mes New Roman', Times, serif; font-weight:normal;color:#000000;font-size:12=
px;padding-bottom:4px;" href=3D"http://enews.thebay.com/a/hBXoKdsB8H8DrB9QZ=
NENsh1Se4N/french">Afficher en fran&ccedil;ais</a></td>
  </tr>
  </table></td>
  </tr>
</table>
<!--------------------------------HEADER TEXT LINK OPTIONS END-------------=
------------------->=20
<!--------------------------------LOGO START-------------------------------=
->
<table width=3D"640" border=3D"0" align=3D"center" cellpadding=3D"0" cellsp=
acing=3D"0" id=3D"HeaderTextLinksEN">
  <tr>
  <td width=3D"640" height=3D"72" align=3D"center" valign=3D"middle" styl=
e=3D"padding-top:10px; padding-bottom:10px; border=3D"0""><a href=3D"http:/=
/enews.thebay.com/a/hBXoKdsB8H8DrB9QZNENsh1Se4N/logo?rremail=3Dhunter003@te=
lus.net&email=3Dhunter003@telus.net&REF_ID=3DEMLHB62719DAILY"><img style=3D=
"display:block;" src=3D"http://f.enews.thebay.com/i/33/2082455787/hudsons-b
Message longer than Log limit (4000). Message was truncated.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
there's a lot of mess before the double quotes that break everything.

try to remove that weirdness with .replace
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
It is incomplete. Write the result to a file instead of using log... Log is limited to 4000 bytes.
Thank you DonManfred. Attached is the text file for a message that doesn't display properly. This is a differrent message from the last, as I have to catch them as I can.

Best regards
 

Attachments

  • Body.txt
    22.1 KB · Views: 290
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
as I have to catch them as I can
All of them have html entities which needs to be replaced...

I´m sure it can be optimized

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private www As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")

    Dim html As String = File.ReadString(File.DirAssets,"body.txt")
    html = strip(html)
    File.WriteString(File.DirRootExternal,"bodynew2.txt",html)
    www.LoadHtml(html)
   
End Sub
Sub strip(value As String) As String
    value = value.Replace("=20"," ")
    value = value.Replace("=21","!")
    value = value.Replace("=22",$"""$)
    value = value.Replace("=23","#")
    value = value.Replace("=24","$")
    value = value.Replace("=25","%")
    value = value.Replace("=26","&")
    value = value.Replace("=27","'")
    value = value.Replace("=28","(")
    value = value.Replace("=29",")")
    value = value.Replace("=2A","*")
    value = value.Replace("=2B","+")
    value = value.Replace("=2C",",")
    value = value.Replace("=2D","-")
    value = value.Replace("=2E",".")
    value = value.Replace("=2F","/")
    value = value.Replace("=30","0")
    value = value.Replace("=31","1")
    value = value.Replace("=32","2")
    value = value.Replace("=33","3")
    value = value.Replace("=34","4")
    value = value.Replace("=35","5")
    value = value.Replace("=36","6")
    value = value.Replace("=37","7")
    value = value.Replace("=38","8")
    value = value.Replace("=39","9")
    value = value.Replace("=3A",":")
    value = value.Replace("=3B",";")
    value = value.Replace("=3C","<")
    value = value.Replace("=3E",">")
    value = value.Replace("=3F","?")
    value = value.Replace("=40","@")
    value = value.Replace("=3D","=")
    Return value
End Sub
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
All of them have html entities which needs to be replaced...

I´m sure it can be optimized
Thank you for your reply. I tried your stripping method, and while the message display was altered it still did not display as hoped. I have attached four files showing the message before and after stripping. The image to the left is before stripping. This may be a limitation of WebView, and it may be not possible to change this behavior.

Best regards

 

Attachments

  • BodyNoStrip.txt
    57.7 KB · Views: 280
  • BodyStrip.txt
    56 KB · Views: 284
Upvote 0

sorex

Expert
Licensed User
Longtime User
it didn't strip all of them thus causing problems.

how come that weirdness is inthere? it aren't real html entities at all.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
this fixes most of it.

B4X:
html=File.ReadString(File.DirAssets,"BodyNoStrip.txt")
html=html.Replace(Chr(0x3d) & Chr(0xd) & Chr(0xa),"")
html=html.Replace("3D","")
html=html.Replace("=09",TAB)
html=html.Replace("=2C",",")

 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Thank you sorex. I tried your suggestion and most of these messages now display well. I have attached three samples of messages that still do not. The images are ordered Body1 to Body3 starting on the left. Perhaps with another look you will see a means of further improvement.

These messages are all sent by a commercial entity for advertising purposes, and are displayed correctly in Thunderbird. So, I think there must be some standard that is being adhered to. I appreciate your help.

Best regards
 

Attachments

  • Body1.txt
    15.7 KB · Views: 237
  • Body2.txt
    20.9 KB · Views: 244
  • Body3.txt
    140.5 KB · Views: 247
Upvote 0

sorex

Expert
Licensed User
Longtime User
you should question where the weirdness is getting added.

are you copy pasting the mail content in the mail client? or via right click > view source > copy ?
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
you should question where the weirdness is getting added.

are you copy pasting the mail content in the mail client? or via right click > view source > copy ?
Hello sorex. When these emails are viewed in Thunderbird they have been downloaded from the mail server in the normal manner, and are displayed correctly. In my app I am downloading mail from the server using the Net Library and the MailParser. I am using M.Body from the Mailparser. M.Body is stored in an Array from which the string MsgBody is ultimately created. The string MsgBody is displayed using WebViewBody.LoadHtml(MsgBody). I don't think anything in this process would introduce changes to the Mailparser's M.Body.

Best regards
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
what type of encoding do you use when the mail is being retrieved?
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
what type of encoding do you use when the mail is being retrieved?
Hello sorex. I haven't made provision for an encoding requirement. The Mailparser is brought into play with the (simplified) code below. Should I be making some provision for encoding?

Best regards
B4X:
'Parse the mail
'Log("currentMessage = " & currentMessage & " messageCount = " & messageCount)
Dim M As Message
M = MailParser.ParseMail(MessageText, File.DirRootExternal)
'Log("M = " & M)
arrayBody(MsgIndex) = M.Body
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I never used the pop3 stuff in B4A but aren't you using this

B4X:
Sub EventName_DownloadCompleted (Success As Boolean, MessageId As Int, Message As String)
Message.GetBytes("UTF8")    'try some iso ascii encoding here
End Sub

worst case extend DonManfreds sub with my lines, put it at the top or it will fail due to wrong ordering of the replacements.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
My thanks to both DonManfred and sorex. @sorex - I may be wrong about this, as I was wrong once before. (chuckle) But, I think these messages are being received in UTF8. I took your worst case suggestion and extended DonManfred's Sub with your additional lines. This seems to be working well. Time will tell if the issue is completely resolved. I need to gain more knowledge than I have with email, HTML etc. I appreciate the help I receive on this forum.

Best regards
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
I guess one should never say, an issue is now completely resolved, until it's confirmed. I have now found a message that will not display at all. It was completely stripped of all characters. Image and text file attached. Perhaps Erel can add something to the conversation. There has to be something I have missed in my use of the Mailparser.

Best regards

Edit: This is getting interesting. I have added BodyRogers3.txt downloaded by my app with no stripping. As you can see it's blank. Yet, as seen in image BodyRogersClient, it displays in Thunderbird just fine.

 

Attachments

  • BodyRogers.txt
    0 bytes · Views: 236
  • BodyRogers3.txt
    0 bytes · Views: 249
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…