Send HTML tables with SendMail

I'm trying to send some HTML table using Mathematica SendMail function. To do it I wrote this function:

createHTMLTable[list_List] /; Length[Dimensions@list] == 2 :=
data = Rest@list;
data = Map["   <td>" <> ToString[#] <> "</td>\n" &, data, {2}];
r = "<tr>\n" <> StringJoin[##] <> "</tr>\n" & /@ headData // StringJoin;
r = "<!DOCTYPE html>
<html>
<body>
<table border=\"1\">
" <> r <> "
</table>
</body>
</html>"
]


When I send the mail, as in this example:

list = RandomInteger[100000, {10, 3}]

SendMail["From" -> "test@test.com", "To" -> "test@test.com",
"Subject" -> "Sending Email from Mathematica", "Body" -> createHTMLTable[list],
"Server" -> "XXXXX"]


there is no table in the mail, but only the HTML text. Anyone knows how to correct this? Is there another way to do it?

-
Can you check saving createHTMLTable[list] to a file and then opening it with a browser ? – b.gatessucks Nov 6 '12 at 8:09
No.. I need it in the mail body. Today I use TableForm but Mathematica converts it into a image in the body. – Murta Nov 6 '12 at 10:47
I meant to check if your function produces a correct HTML file; maybe the mistake is there rather than in SendMail. – b.gatessucks Nov 6 '12 at 10:58
Ahh.. now I get. Yes, it works ok. – Murta Nov 6 '12 at 11:18
Could you change the Wolfram email addresses into something more innocent? We don't want to spam WRI with our test mails, do we? Also: isn't there a // StringJoin too much in your code? – Sjoerd C. de Vries Nov 6 '12 at 13:33

Using JLink and Apache Commons Email and Java Mail it is not that hard to get MIME controlling working. I just modified some code I wrote some time ago (mostly for being able to send Email from within webMathematica) and added the ability to send HTML emails. It is a whole package with the jar files in subfolder and a Notebook with an example, so I hope it is o.k. if I point interested users to my Mercurial repository here (zip download enabled):

http://code.gluonvision.com/SendEmail/

An HTML example is in SendMail.nb

Let me know if it works for you.

Rolf

-
Excelent! I'll try it. Just a small correction. In your SendMail.nb, you have one " after To in your example `SendMail code. And a comma is missing. – Murta Nov 9 '12 at 10:40
Glad it works. The type is fixed. I uploaded a new revision. That just happened when I uncommented my password ... – Rolf Mertig Nov 9 '12 at 11:29
Unfortunately not.. I tried in Windows and Mac. I get this msg: Java::excptn: "A Java exception occurred: "org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:25\\ n \\ tat org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)\\ n \\ tat org.apache.commons.mail.Email.send(Email.java:1267)\\ nCaused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. u21sm12128247yhl.6" – Murta Nov 9 '12 at 19:03
Did you use "PortNumber" -> 465 ? – Rolf Mertig Nov 9 '12 at 19:40
No I have used my original google server port. Yes!.. now it works! Tks a lot! You have my point. – Murta Nov 10 '12 at 2:49