HTML在电子邮件模板上没有渲染,而是显示为文本。

huangapple 未分类评论47阅读模式
英文:

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(&quot;Sending Expiry email to Products team. txId=\&quot;{}\&quot;&quot;, retrospection.getTxId());		
		boolean mailSent = true;		
		try {
			JavaMailSender javaMailSender = emailConfig.getMailSender();			
			MimeMessage mimeMsg = prepareMailForExpairy(retrospection, javaMailSender);
			javaMailSender.send(mimeMsg);			
		}catch (MailException me) {
			log.error(&quot;Error occurred while sending an email &quot;, 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, &quot;utf-8&quot;);
		String text = createEmialTemplate(retrospection);
		
		MimeMultipart content = new MimeMultipart();
	    MimeBodyPart html = new MimeBodyPart();
	    html.setContent(text, &quot;text/html&quot;);
	    html.setHeader(&quot;MIME-Version&quot; , &quot;1.0&quot; );
	    html.setHeader(&quot;Content-Type&quot; , 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(&quot;&lt;html&gt;&quot; +
	                   &quot;&lt;body&gt;&quot; +
					   &quot;&lt;div style=&#39;overflow-x:auto;margin-left: auto; margin-right: auto; width:50%&#39;&gt;&quot; +
					   &quot;&lt;h2 align=&#39;left&#39;&gt;Expiry Summary Report&lt;/h2&gt;&quot;+
	                   &quot;&lt;table align=&#39;left&#39; cellspacing=&#39;2&#39; style=&#39;border-width: thick; margin: 0px auto;&#39; height=130px; width=60% border=&#39;3&#39; runat=&#39;server&#39;&gt;&quot; +
					   &quot;&lt;tbody&gt;&quot;+
					   &quot;&lt;tr&gt;&quot;+
	                   &quot;&lt;th&gt;Failure Asset Id&lt;/th&gt;&quot;+
					   &quot;&lt;th&gt;Expiry Date&lt;/th&gt;&quot;+
	                   &quot;&lt;/tr&gt;&quot;+
					   &quot;&lt;tr&gt;&quot;+
	                   &quot;&lt;td&gt;&quot;XYZ&quot;&lt;/td&gt;&quot;+
	                   &quot;&lt;td&gt;&quot;2020-04-05&quot;&lt;/td&gt;&quot;+
	                   &quot;&lt;tr&gt;&quot;+
	                   &quot;&lt;/tbody&gt;&quot;+
	                   &quot;&lt;/table&gt;&quot;+
	                   &quot;&lt;/div&gt;&quot;+
	                   &quot;&lt;/body&gt;&quot;+
	                   &quot;&lt;/html&gt;&quot;);	
		}
		
		if(null != rca) {
			builder.append(&quot;Root Cause Analysis &quot;)
				.append(rca.toJson());
		}
		return builder.toString();
	}

After running the application after the mail received the email on outlook, the email template looks like this

&lt;html&gt;&lt;body&gt;&lt;div style=&#39;overflow-x:auto;margin-left: auto; margin-right: auto; width:50%&#39;&gt;&lt;h2 align=&#39;left&#39;&gt;Expiry Summary Report&lt;/h2&gt;&lt;table align=&#39;left&#39; cellspacing=&#39;2&#39; style=&#39;border-width: thick; margin: 0px auto;&#39; height=130px; width=60% border=&#39;3&#39; runat=&#39;server&#39;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Expiry Date&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;xyz&lt;/td&gt;&lt;td&gt;2020-04-26&lt;/td&gt;&lt;tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;

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,&quot;text/html&quot;);

答案2

得分: 0

请参考此链接以了解getContentType的工作原理。因此根据定义,由于您之前未设置过值,它将返回"text/plain"。使用以下代码行设置内容类型值。

html.setHeader(&quot;Content-Type&quot; , &quot;text/html&quot;);

希望能对您有所帮助。

英文:

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(&quot;Content-Type&quot; , &quot;text/html&quot;);

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.

huangapple
  • 本文由 发表于 2020年5月4日 22:54:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61595163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定