我已经编写了用于发送电子邮件通知的代码,电子邮件已正确发送到Outlook,但是内容显示为文本而不是html,无法找到导致HTML代码在Outlook中无法呈现为html的根本原因。>
public boolean sendEmailForExpireIssue(Retrospection retrospection) throws EmailException, MessagingException { log.info("Sending Expiry email to Products team. txId=\"{}\"", retrospection.getTxId()); boolean mailSent = true; try { JavaMailSender javaMailSender = emailConfig.getMailSender(); MimeMessage mimeMsg = prepareMailForExpairy(retrospection, javaMailSender); javaMailSender.send(mimeMsg); }catch (MailException me) { log.error("Error occurred while sending an email ", me); mailSent = false; throw new EmailException(HttpStatus.INTERNAL_SERVER_ERROR, me.getMessage()); } return mailSent; } private MimeMessage prepareMailForExpairy(Retrospection retrospection, JavaMailSender javaMailSender) throws MessagingException { //Session session = null; MimeMessage mimeMsg = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, "utf-8"); String text = createEmialTemplate(retrospection); MimeMultipart content = new MimeMultipart(); MimeBodyPart html = new MimeBodyPart(); html.setContent(text, "text/html"); html.setHeader("MIME-Version" , "1.0" ); html.setHeader("Content-Type" , html.getContentType() ); content.addBodyPart(html); mimeMsg.setContent(content); helper.setFrom(emailConfig.getSender()); helper.setTo(emailConfig.getRecipients()); helper.setSubject(RetrospectionConstants.EmailConstants.EXPIRY_MAIL_SUB); helper.setText(text); helper.setSentDate(new Date()); return mimeMsg; } private String createEmialTemplate(Retrospection retrospection) { Asset asset = Asset.fromJson(retrospection.getAsset()); Info info = asset.getInfo(); ErrorResponse rca = ErrorResponse.fromJson(retrospection.getErrorResponse()); StringBuilder builder = new StringBuilder(); if(null != info) { builder.append("<html>" + "<body>" + "<div style='overflow-x:auto;margin-left: auto; margin-right: auto; width:50%'>" + "<h2 align='left'>Expiry Summary Report</h2>"+ "<table align='left' cellspacing='2' style='border-width: thick; margin: 0px auto;' height=130px; width=60% border='3' runat='server'>" + "<tbody>"+ "<tr>"+ "<th>Failure Asset Id</th>"+ "<th>Expiry Date</th>"+ "</tr>"+ "<tr>"+ "<td>"XYZ"</td>"+ "<td>"2020-04-05"</td>"+ "<tr>"+ "</tbody>"+ "</table>"+ "</div>"+ "</body>"+ "</html>"); } if(null != rca) { builder.append("Root Cause Analysis ") .append(rca.toJson()); } return builder.toString(); }
[邮件在Outlook中收到电子邮件后运行应用程序后,电子邮件模板如下所示
<html><body><div style='overflow-x:auto;margin-left: auto; margin-right: auto; width:50%'><h2 align='left'>Expiry Summary Report</h2><table align='left' cellspacing='2' style='border-width: thick; margin: 0px auto;' height=130px; width=60% border='3' runat='server'><tbody><tr><th>Id</th><th>Expiry Date</th></tr><tr><td>xyz</td><td>2020-04-26</td><tr></tbody></table></div></body></html>
任何人都可以在这方面帮助我。为了使html可以呈现,我必须添加什么其他代码。
我已经编写了用于发送电子邮件通知的代码,电子邮件已正确发送到Outlook,但是内容以文本而不是html的形式出现,而无法找到导致html代码不正确的根本原因...
MimeBodyPart
的部分。用Mimemessage
直接设置内容以及内容类型。这对我有用: