我可以成功通过Java Mail API发送电子邮件,但现在我正在尝试发送从带有边框等格式的表中的MySQL查询填充的ResultSet的内容,我可以使用CSS标签来执行此操作吗?如果是这样的话?
我的代码如下:
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();
}
}
我可以发送电子邮件,但只能发送纯文字,&#39; sm&#39;是一个SendMail的实例,如下所示:
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();
}
}
答案 0 :(得分:7)
您可以使用inline css来表示您的表格。您不能单独使用css,因为大多数电子邮件服务都不支持它..将您的电子邮件字符串构建代码更改为以下代码。
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();