Android Code Snippet Fighting with sending HTML-content to Outlook. A simple solution for that mess...

Honestly, this is less of a question and more of food for thought for people who (like me) struggle a bit with HTML and Outlook. I'm not even sure if I'm in the right area with this post. If not, please move it...

Primarily, it is about generating a correctly formatted text email with HTML in Outlook according to your wishes, without having to study HTML first. The method is very simple and maybe not that elegant, but honestly: I want to deliver a reliably working B4J programme and not torture myself for hours with the formatting problems of an email in HTML code that is generated via the programme and then sent with Outlook.
The problem is the interpretation of this HTML intent by Outlook itself. You write HTML code, send it to Outlook and it works... unfortunately not, but it does in every browser. It almost seems to me that Outlook (or Microsoft) has set its own rules regarding HTML. Changing a font, for example, does not work at all with the conventional methods.
I spent hours trying to find a working symbiosis between Outlook and HTML code, although the code for this in B4X was ten times larger than the actual content of the email, I didn't really succeed, so that's the end of it.

If you only want to send a text file in the same form with variable data again and again, but want to display this email in Outlook in a sensible format, you can make your life a little easier with the following solution:

1.
Simply write the mail body in MS Word, format the text according to your wishes (fonts, font size changes, style changes, formatting). At the points for variable data, simply insert keywords that will most likely never appear in the normal text.
2.
Save this MS Word file as a HTML file and give this HTML file to your programme via the assets.
3.
Look at the generated HTML file with a text editor. There is a lot of overhead in a HTML file generated by MS Word, many HTML constructions seem to be typical for Microsoft. Delete all unnecessary file descriptions in the HTML file (file name, author, creation date, etc.).
4.
To send the email, read the complete content of the HTML-file from the assets into a string variable and replace the keywords with the actual variables from the programme that should appear in the email using the replace function.
B4X:
MailText = File.ReadString(File.DirAssets,"Your file.html")
MailText = MailText.Replace("THIS IS A PLACEHOLDER STRING","THIS IS THE CONTENT APPEARING WITHIN THE MAIL")
5.
Send this string to Outlook with the usual SMTP-function.

Done. I was surprised how well this really works. Now it is not the philosopher's stone, but it is very helpful if it has to be Outlook...
For creating a "list"-like output (your daily shopping list or whatever) simply append the same piece of HTML-CODE with the changed variables again and again before sending.

Hope this will help other dummies like me...
 
Last edited:

BlueVision

Active Member
Licensed User
Longtime User
Hi Erel, don't know. Originally I wanted to post in the B4J-Section, due to my limited brain capacities it went to the Android-Section. Maybe B4X or simply leave it at this place.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…