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();
}
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With