英文:
HTML is not rendering on email template, coming as text
问题
以下是翻译好的内容:
我已经编写了发送电子邮件通知的代码,电子邮件成功发送到Outlook,但内容显示为文本,而不是HTML,无法找到HTML代码未在Outlook中呈现为HTML的根本原因
public boolean sendEmailForExpireIssue(Retrospection retrospection) throws EmailException, MessagingException {
log.info("向产品团队发送到期电子邮件。txId={}", retrospection.getTxId());
boolean mailSent = true;
try {
JavaMailSender javaMailSender = emailConfig.getMailSender();
MimeMessage mimeMsg = prepareMailForExpairy(retrospection, javaMailSender);
javaMailSender.send(mimeMsg);
} catch (MailException me) {
log.error("发送电子邮件时发生错误", me);
mailSent = false;
throw new EmailException(HttpStatus.INTERNAL_SERVER_ERROR, me.getMessage());
}
return mailSent;
}
private MimeMessage prepareMailForExpairy(Retrospection retrospection, JavaMailSender javaMailSender) throws MessagingException {
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'>到期摘要报告</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>到期日期</th>" +
"</tr>" +
"<tr>" +
"<td>XYZ</td>" +
"<td>2020-04-05</td>" +
"</tr>" +
"</tbody>" +
"</table>" +
"</div>" +
"</body>" +
"</html>");
}
if (null != rca) {
builder.append("根本原因分析 ")
.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'>到期摘要报告</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>到期日期</th></tr><tr><td>xyz</td><td>2020-04-26</td><tr></tbody></table></div></body></html>
请问有谁能帮助我。我需要添加哪些额外的代码,以便HTML能够呈现。
英文:
I have written code for sending email notification, email is properly sending to outlook but content is appearing as text rather than html not able to find the root cause why html code is not rendering as html in outlook
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();
}
After running the application after the mail received the email on outlook, the email template looks like this
<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>
Can anyone please help me on this. What is the additional code I have to add so that the html can render.
答案1
得分: 0
直接在设置 MimeMessage
的内容时跳过设置 MimeBodyPart
的部分。可以使用以下代码来设置内容和内容类型,这对我起作用了:
message.setContent(htmlString, "text/html");
英文:
You can skip the part where you set MimeBodyPart
. Directly set your content with Mimemessage
along with content type. This worked for me :
message.setContent(htmlString,"text/html");
答案2
得分: 0
请参考此链接以了解getContentType的工作原理。因此根据定义,由于您之前未设置过值,它将返回"text/plain"。使用以下代码行设置内容类型值。
html.setHeader("Content-Type" , "text/html");
希望能对您有所帮助。
英文:
Please refer this link to understand how getContentType works. So by the definition it will return "text/plain" as you have not set the value previously. Set the content-type value using the following line of code.
html.setHeader("Content-Type" , "text/html");
Hope this helps.
答案3
得分: 0
.setText()
方法的MimeMessageHelper
接受两个参数:文本本身和一个布尔值,用于指示文本是否为HTML。
因此,在你的prepareMailForExpairy
方法中,你需要将这行代码:
helper.setText(text);
修改为:
helper.setText(html, true);
英文:
The .setText() method of the MimeMessageHelper accepts 2 Arguments: The Text itself and a boolean if the text is html.
Therefore, you need to change the line
helper.setText(text);
to
helper.setText(html, true);
in your prepareMailForExpairy method.
专注分享java语言的经验与见解,让所有开发者获益!
评论