Android Question Emails smtp with html in the body don't work :(

Luis Miguel T. Martins

Member
Licensed User
Longtime User
Because this don't work?

Dim SMTP As SMTP

SMTP.HtmlBody = True



SMTP.Initialize("smtp.gmail.com", 465, "[email protected]", "password", "SMTP")
SMTP.UseSSL = True

SMTP.To.Add("[email protected]")
SMTP.Subject = "Test"
SMTP.Body = "<!DOCTYPE html><html><body><table border=1><tr><td>teste</td></tr></table></body></html>"


SMTP.Send


The result is:"<!DOCTYPE html><html><body><table border1><tr><td>teste</td></tr></table></body></html>"

and not the table html :( thanks
 

Peter Simpson

Expert
Licensed User
Longtime User
Hello @Luis Miguel T. Martins, try the code below, you should find that it works perfect for you

B4X:
    Dim SMTPClient As SMTP
    SMTPClient.Initialize("smtp.gmail.com", 465, "[email protected]", "yourpassword", "SMTP")

    SMTPClient.Sender = "[email protected]"
    SMTPClient.To.Add([email protected])

    SMTPClient.UseSSL = True '465 for Gmail
    SMTPClient.HtmlBody = True

    SMTPClient.Subject = "Test Subject"
    SMTPClient.Body = "<!DOCTYPE html><html><body><table border=1><tr><td>teste</td></tr></table></body></html>"
    SMTPClient.Send

I just tried this and it works perfect. I just received an email with the word teste in the body and nothing else :)
 
Last edited:
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Me too. Aren't you using a "special" email client ???
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
So maybe its value is reset to false?
could be. I´m not using smtp much in my apps. So i dont know. But i believe false is the default-value...
It´s less than a minute to find out if the command work after initalize. Did you tried it?
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
could be. I´m not using smtp much in my apps. So i dont know. But i believe false is the default-value...
It´s less than a minute to find out if the command work after initalize. Did you tried it?
Now i tried it; its exactly like i thought before - the value is set to false after initialize.
 
Upvote 0

Luis Miguel T. Martins

Member
Licensed User
Longtime User
Y
Hello @Luis Miguel T. Martins, try the code below, you should find that it works perfect for you

B4X:
    Dim SMTPClient As SMTP
    SMTPClient.Initialize("smtp.gmail.com", 465, "[email protected]", "yourpassword", "SMTP")

    SMTPClient.Sender = "[email protected]"
    SMTPClient.To.Add([email protected])

    SMTPClient.UseSSL = True '465 for Gmail
    SMTPClient.HtmlBody = True

    SMTPClient.Subject = "Test Subject"
    SMTPClient.Body = "<!DOCTYPE html><html><body><table border=1><tr><td>teste</td></tr></table></body></html>"
    SMTPClient.Send

I just tried this and it works perfect. I just received an email with the word teste in the body and nothing else :)



This work, but without the line "SMTPClient.Sender = "[email protected]"" and the html code "border=1" in the email is border 1 (he delete the = caracter, very strange) but thank you very :)
 
Upvote 0

Luis Miguel T. Martins

Member
Licensed User
Longtime User
this work with the border, perfect, I can't input = then I replace for &#60 thanks for all

B4X:
corpo="<!DOCTYPE html><html><body><table border &#60 0 cellspacing &#60 1 cellpadding &#60 '0' bgcolor &#60 #CCCCCC><tr><td>"&title&"</td><td>Value:</td></tr>"



Dim SMTPClient As SMTP
                          SMTPClient.Initialize("smtp.gmail.com", 465, "[email protected]", "*************", "SMTP")

                          
                           SMTPClient.To.Add(email_destino)

                           SMTPClient.UseSSL = True '465 for Gmail
                           SMTPClient.HtmlBody = True

                           SMTPClient.Subject = "OORCAA"
                           SMTPClient.Body = corpo
                           SMTPClient.Send
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
What I personally do is really simply.
I create an actual populated HTML document in Dreamweaver and save it into the assets folder. If necessary I embed keywords like #Name, #Address, #ZipCode, #Telephone etc that can be replaced with an actual name, Address, ZipCode, Telephone Number when reading into SMTPClient.Body. I then read the HTML file into SMTPClient.Body replacing #Name, #Address etc if I have including them in the HTML file.

That's it, it's easy to do and you do not have to worry about double quotes and guess work in your actual B4A code. Plus you can get the HTML side of things looking really nice and fancy before loading the html file into the SMTPClient.Body.

Enjoy...
 
Last edited:
Upvote 0

Tron

Member
Licensed User
Longtime User
@Peter Simpson :
I 100% agree in this method , since I also used a "replacement" string in my example/snippet.
e.g ($001).
B4X:
   :
' change Background color to #e3efff
BodyTemp = BodyTemp.Replace ("$001","#e3efff")
SMTP.Body = BodyTemp
  :
The html template contains a $001 for changing the backgound color.
If more infos should be variable, define more placeholders in the html template. e.g. $002 , $003 ...

greets Jens
 
Last edited:
Upvote 0
Top