Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Mail Formatting a table sent in an e-mail using CSS?

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?

My code is as follows:

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("<html><head><style type='text/css'>table .out {border-width:1px, "
                    + "border-color: black}</style></head>"
                    + "<body>"
                    + "<table class'out'><span style=border-color: black, border-width: 1px>");

        update.setString(1, "Outstanding");
        ResultSet results = update.executeQuery();

        while (results.next()) {
            System.out.println("in results...");
            email.append("<tr>");
            email.append("<td>");
            long period = results.getLong("period_to");
            email.append(DateConvert.fromEpoch(period));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("type"));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("amt"));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("status"));
            email.append("</td>");

            email.append("<tr>");
        }

        email.append("</table></body></html>");
        sm.populateMailMessage(email.toString());
        sm.sendMail();
    } catch (SQLException sql) {
        sql.printStackTrace();
    }                 
}

I am able to send the email but only plain text, 'sm' is an instances of SendMail as below:

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 ===> 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 ===> 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 ===> 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();
    }
}
like image 929
mg3np1 Avatar asked Nov 02 '14 23:11

mg3np1


1 Answers

you can use inline css for style your table .you can't use css separatly since most email services don't support it ..try to change your email String building code to following code.

 email.append("<html><body>"
                    + "<table style='border:2px solid black'>");

        update.setString(1, "Outstanding");
        ResultSet results = update.executeQuery();

        while (results.next()) {
            System.out.println("in results...");
            email.append("<tr bgcolor=\"#33CC99\">");
            email.append("<td>");
            long period = results.getLong("period_to");
            email.append(DateConvert.fromEpoch(period));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("type"));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("amt"));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("status"));
            email.append("</td>");

            email.append("<tr>");
        }

        email.append("</table></body></html>");
        sm.populateMailMessage(email.toString());
        sm.sendMail();
like image 91
Madhawa Priyashantha Avatar answered Nov 14 '22 22:11

Madhawa Priyashantha