title: Java Mail Formatting a table sent in an e-mail using CSS?
body: <p>I can success you the Java Mail API to send e-mails but now I am trying to sending the contents of a ResultSet populated from a MySQL query in table formatted with borders etc, can I use CSS tags to do this? if so how?</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>public void getOutstanding() throws MessagingException {
      try {
          String outS = "SELECT period_to, type, amt, status FROM tblinstall "
                          + "WHERE status like ?";
  
          PreparedStatement update = toDB.prepareStatement(outS);
  
          email = new StringBuilder();
  
          email.append("&lt;html&gt;&lt;head&gt;&lt;style type='text/css'&gt;table .out {border-width:1px, "
                      + "border-color: black}&lt;/style&gt;&lt;/head&gt;"
                      + "&lt;body&gt;"
                      + "&lt;table class'out'&gt;&lt;span style=border-color: black, border-width: 1px&gt;");
  
          update.setString(1, "Outstanding");
          ResultSet results = update.executeQuery();
  
          while (results.next()) {
              System.out.println("in results...");
              email.append("&lt;tr&gt;");
              email.append("&lt;td&gt;");
              long period = results.getLong("period_to");
              email.append(DateConvert.fromEpoch(period));
              email.append("&lt;/td&gt;");
  
              email.append("&lt;td&gt;");
              email.append(results.getString("type"));
              email.append("&lt;/td&gt;");
  
              email.append("&lt;td&gt;");
              email.append(results.getString("amt"));
              email.append("&lt;/td&gt;");
  
              email.append("&lt;td&gt;");
              email.append(results.getString("status"));
              email.append("&lt;/td&gt;");
  
              email.append("&lt;tr&gt;");
          }
  
          email.append("&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;");
          sm.populateMailMessage(email.toString());
          sm.sendMail();
      } catch (SQLException sql) {
          sql.printStackTrace();
      }                 
  }
  </code></pre>
  
  <p>I am able to send the email but only plain text, 'sm' is an instances of SendMail as below:</p>
  
  <pre><code>public class SendMail {
      private Properties mailAccessCredentials;
      private Session mailSession;
      private MimeMessage mailMessage;        
  
      public SendMail() throws MessagingException{
          populateProperties();
      }    
  
      private void populateProperties() throws AddressException, MessagingException {     
          //Step1    
          System.out.println("\n 1st ===&gt; setup Mail Server Properties..");
          mailAccessCredentials = System.getProperties();
          mailAccessCredentials.put("mail.smtp.port", "587"); // TLS Port
          mailAccessCredentials.put("mail.smtp.auth", "true"); // Enable Authentication
          mailAccessCredentials.put("mail.smtp.starttls.enable", "true"); // Enable StartTLS
          System.out.println("Mail Server Properties have been setup successfully..");     
      }    
  
      public void populateMailMessage(String msg) throws MessagingException{               
          //Step2    
          System.out.println("\n\n 2nd ===&gt; get Mail Session..");
          mailSession = Session.getDefaultInstance(mailAccessCredentials, null);
          mailMessage = new MimeMessage(mailSession);
          mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("n****@gmail.com"));
  
          mailMessage.setSubject("Report from Database Payments now Due");
  
          mailMessage.setContent(msg, "text/html");
          System.out.println("Mail Session has been created successfully..");
      }
  
      public void sendMail() throws MessagingException{        
          //Step3    
          System.out.println("\n\n 3rd ===&gt; Get Session and Send mail");
          Transport transport = mailSession.getTransport("smtp");
  
          // Enter your correct gmail UserID and Password
          transport.connect("smtp.gmail.com", "lettingssmart@******", "****");
          transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
          transport.close();
      }
  
      public static void main(String[] args) throws MessagingException {
          SendMail sm = new SendMail();
          //sm.populateMailMessage();
          sm.sendMail();
      }
  }
  </code></pre>