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

Luis Miguel T. Martins

Member
Licensed User
Because this don't work?

Dim SMTP As SMTP

SMTP.HtmlBody = True



SMTP.Initialize("smtp.gmail.com", 465, "myemail@gmail.com", "password", "SMTP")
SMTP.UseSSL = True

SMTP.To.Add("emailto@email.com")
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
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, "yourgmailemailaddress@gmail.com", "yourpassword", "SMTP")

    SMTPClient.Sender = "youremailaddress@xxx.com"
    SMTPClient.To.Add(toemailaddress@blahblahblah.com)

    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:

lemonisdead

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

rboeck

Well-Known Member
Licensed 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.
 

Luis Miguel T. Martins

Member
Licensed 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, "yourgmailemailaddress@gmail.com", "yourpassword", "SMTP")

    SMTPClient.Sender = "youremailaddress@xxx.com"
    SMTPClient.To.Add(toemailaddress@blahblahblah.com)

    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 = "youremailaddress@xxx.com"" and the html code "border=1" in the email is border 1 (he delete the = caracter, very strange) but thank you very :)
 

Luis Miguel T. Martins

Member
Licensed 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, "xxxxx@gmail.com", "*************", "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:

Peter Simpson

Expert
Licensed 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:

Tron

Member
Licensed 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:
Top