gpt4 book ai didi

Java Mail 使用 CSS 格式化电子邮件中发送的表格?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:05:33 26 4
gpt4 key购买 nike

我可以成功地使用 Java Mail API 发送电子邮件,但现在我正在尝试发送一个 ResultSet 的内容,该结果集是从带有边框等格式的表格中的 MySQL 查询填充的,我可以使用 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();
}
}

我可以发送电子邮件,但只能发送纯文本,“sm”是 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();
}
}

最佳答案

你可以使用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();

关于Java Mail 使用 CSS 格式化电子邮件中发送的表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26705602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com