B4J Question HTML email not sending correclty

Discussion in 'B4J Questions' started by aaronk, Jul 5, 2015.

  1. aaronk

    aaronk Well-Known Member Licensed User


    I am trying to send a HTML email using B4J.

    I have worked out how to send the email etc. but for some reason it's not displaying the email correctly.

    I have a HTML page (demo.html) that I created using notepad and then I am opening that file in B4J and loading it into a string.

    I then am converting variables in that file before sending it.

    Here is my B4J Code I am using:

    Sub SendEmail(ToEmail As String, Subject As String, body As String)

    SMTP.Sender = "Testing" & "<" & "FROM email is here" & ">"
    SMTP.HtmlBody = True
    SMTP.Body = body
    SMTP.Subject = Subject
    End Sub

    Sub SMTP_MessageSent(Success As Boolean)
    If Success Then
    log("email sent")
    End If
    End Sub

    Sub FormatEmail(username As String)
    Dim MainPage As String = GetTemplate("demo.html"'load the email template

        MainPage = MainPage.Replace(

    "The TO email is here","The Subject here",MainPage)

    End Sub

    Sub GetTemplate(Name As StringAs String
    If templates.ContainsKey(Name) Then Return templates.Get(Name)
    Dim temp As String = File.ReadString(File.DirApp & "/email_templates", Name)
        templates.Put(Name, temp)
    Return temp
    End Sub
    Based on the code above, I run the Sub 'FormatEmail' and it will then then open the demo.html page and replace the variables with something else and then send the email.

    The problem is that the email looks like the following when viewing it in the inbox:


    In fact it should look like the following HTML page:


    Is there something I have missed in my B4J code to make it send the HTML page correctly ?

    If I use the same HTML code (demo.html) and send it using another method (such as a email client) it sends the email fine. It only seems to have the issue using B4J to send the email.

    Any one know what I have missed ?
    (images and text in the sample email above is for just testing)
  2. Roycefer

    Roycefer Well-Known Member Licensed User

    It looks like a lot of formatting information was stripped from the HTML file. Does demo.html rely on any external CSS files?
  3. aaronk

    aaronk Well-Known Member Licensed User

    No. It only has 1 png file that is external. (as per my screenshot the B4J logo is the only external file everything else is HTML or CSS that is in the same HTML page)

    All CSS code is within the page.

    If I log the body of the email that is being sent it includes all the HTML code including the CSS code in that same HTML page.
  4. Roycefer

    Roycefer Well-Known Member Licensed User

    It looks like you're opening the email in Gmail. Are you in Chrome? If so, while in your Gmail window and with that email displayed, right-click on the email and click on "Inspect Element". You should be able to see the full DOM of the page, including your displayed email. If there are any discrepancies, that's where you'd find them. I know Internet Explorer has a similar function and I imagine FireFox must, too, but I don't recall how to access them off-hand.
  5. aaronk

    aaronk Well-Known Member Licensed User

    Yes your correct, I am viewing this email in Gmail (well Google Mail since I have a purchased version of Gmail).

    If I click the down arrow and click 'Show original' it does show the CSS and HTML code. So looks like it got sent but for some reason isn't displaying in the email.


    However, If I copy the HTML code from the HTML page I am sending and paste it in a Email client (or even from my WordPress site using a plugin to send the email) and then send the email, the email then displays correctly in Gmail.

    Something in B4J is causing it not to display correctly but can't work it :(
  6. aaronk

    aaronk Well-Known Member Licensed User

    Umm.. Just tried sending the email to another email that doesn't use Gmail and it seems to display correctly.

    Seems that for some reason Gmail doesn't like the email I am sending for some reason.

    Find it a little weird that the same HTML code I am using works when using a wordpress plugin to send it and displays fine in Gmail.
    I guess I will need to play around with it and change my template around.
  7. Reinierus

    Reinierus Member Licensed User

    Hello. I have the same problem. The HTML code seems good in Thunderbird email client, but in Outlook not.
    When I send an HTML code, in Thunderbird looks like this:
    In Outlook looks like this:
    I don't know why the code in Outlook is cropped
    It is a bug?
    If it is, there is another way to send HTML emails?

    Thanks a lot
  8. Reinierus

    Reinierus Member Licensed User

    Any idea??
  9. Erel

    Erel Administrator Staff Member Licensed User

    It is always recommended to start a new thread for your question.
  10. Reinierus

    Reinierus Member Licensed User

  11. aaronk

    aaronk Well-Known Member Licensed User

    Just an update on this..

    By adding the following code seems to fix my issue.

    YourMessageString = YourMessageString.Replace("=""=3D")
    mindful likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice