Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 := 
 Module[{r, head, data}, head = First@list;
  data = Rest@list;
  head = Map["   <th>" <> ToString[#] <> "</th>\n" &, {head}, {2}];
  data = Map["   <td>" <> ToString[#] <> "</td>\n" &, data, {2}];
  headData = Join[{head}, data];
  r = "<tr>\n" <> StringJoin[##] <> "</tr>\n" & /@ headData // StringJoin;
  r = "<!DOCTYPE html>
                <table border=\"1\">
                    " <> r <> "

When I send the mail, as in this example:

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

SendMail["From" -> "", "To" -> "", 
 "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?

share|improve this question
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
up vote 5 down vote accepted

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):

An HTML example is in SendMail.nb

Let me know if it works for you.


share|improve this answer
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 :\\ n \\ tat org.apache.commons.mail.Email.sendMimeMessage(\\ n \\ tat org.apache.commons.mail.Email.send(\\ 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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.