我正在尝试通过构建一个字符串并将其作为参数传递给我的邮件方法来发送 HTML 电子邮件:
// Build string to send as email message
StringBuilder buf = new StringBuilder();
buf.append("<html>" +
"<body>" +
"<table>" +
"<tr style=\"background-color: #8C0000; color: #fff; font-family: Arial; font-size: 14px; font-weight: bold;\">" +
"<th style=\"padding: 10px;\">Assigned Group</th>" +
"<th style=\"padding: 10px;\">Incident Type</th>" +
"<th style=\"padding: 10px;\">Status</th>" +
"<th style=\"padding: 10px;\">Assignee</th>" +
"<th style=\"padding: 10px;\">Incident Number</th>" +
"<th style=\"padding: 10px;\">Priority</th>" +
"<th style=\"padding: 10px;\">Summary</th>" +
"</tr>");
for (int i = 0; i < incidentNumberList.size(); i++) {
buf.append("<tr style=\"font-family: Arial; font-size: 12px;\"><td style=\"padding: 10px;\">")
.append(assignedGroup)
.append("</td><td style=\"padding: 10px;\">")
.append(incidentTypeList.get(i))
.append("</td><td style=\"padding: 10px;\">")
.append(statusList.get(i))
.append("</td><td style=\"padding: 10px;\">")
.append(assigneeNameList.get(i))
.append("</td><td style=\"padding: 10px;\">")
.append(incidentNumberList.get(i))
.append("</td><td style=\"padding: 10px;\">")
.append(priorityList.get(i))
.append("</td><td style=\"padding: 10px;\">")
.append(summaryList.get(i))
.append("</td></tr>");
}
buf.append("</table>" +
"</body>" +
"</html>");
String emailMessage = buf.toString();
// Send an email to the manager of the Assigned Group
sendManagerEmail(emailMessage);
电子邮件方式:
private static void sendManagerEmail(String emailMessage) {
final String username = "user";
final String password = "pass123";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
if (ENCRYPT) {
props.put("mail.smtp.starttls.enable", "true");
} else {
props.put("mail.smtp.starttls.enable", "false");
}
props.put("mail.smtp.ssl.trust", "internetsmtp.example.com");
props.put("mail.smtp.host", "internetsmtp.example.com");
props.put("mail.smtp.port", "25");
// SSL
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("martin@example.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("martin@example.com"));
message.setSubject("Tickets — Attention Needed");
message.setContent(emailMessage
, "text/html; charset=utf-8");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
这里重要的一行是message.setContent(emailMessage
, "text/html; charset=utf-8");
我发送到此方法中的 HTML 代码字符串包含消息内容的正文。
电子邮件带有表格格式,但没有 CSS,这使得它非常难看。我可以在这里做些什么来显示 CSS 吗?我认为将样式直接构建到 html 标记中就足够了。
最佳答案
以上代码是正确的,但请确保使用 TLS ( http://blog.redturtle.it/2011/10/28/using-javamail-with-tls ) 加密您的电子邮件,以免它们最终成为垃圾邮件。垃圾邮件通常不呈现 HTML。如果您在设置 TLS 时遇到问题,请通过将邮件从垃圾邮件移至收件箱进行调试以查看呈现的 HTML。
关于java - 尝试使用 JavaMail 通过 Outlook 在 HTML 电子邮件中发送 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42763609/