I can successfully use the Java Mail API to send email, but now I'm trying to send the contents of a ResultSet populated from a MySQL query, in a table formatted with borders, etc., can I use CSS tags for 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 can send an email, but only plain text, "sm" are SendMail instances, as shown below:
public class SendMail {
private Properties mailAccessCredentials;
private Session mailSession;
private MimeMessage mailMessage;
public SendMail() throws MessagingException{
populateProperties();
}
private void populateProperties() throws AddressException, MessagingException {
System.out.println("\n 1st ===> setup Mail Server Properties..");
mailAccessCredentials = System.getProperties();
mailAccessCredentials.put("mail.smtp.port", "587");
mailAccessCredentials.put("mail.smtp.auth", "true");
mailAccessCredentials.put("mail.smtp.starttls.enable", "true");
System.out.println("Mail Server Properties have been setup successfully..");
}
public void populateMailMessage(String msg) throws MessagingException{
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{
System.out.println("\n\n 3rd ===> Get Session and Send mail");
Transport transport = mailSession.getTransport("smtp");
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.sendMail();
}
}
source
share