【问题标题】:HTML is not rendering on email template, coming as textHTML 未在电子邮件模板上呈现,以文本形式出现
【发布时间】:2020-05-04 14:54:38
【问题描述】:

我已经编写了发送电子邮件通知的代码,电子邮件正确发送到 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。

【问题讨论】:

标签: java html spring-boot mail-server


【解决方案1】:

您可以跳过设置MimeBodyPart 的部分。使用Mimemessage 直接设置您的内容以及内容类型。这对我有用:

message.setContent(htmlString,"text/html");

【讨论】:

    【解决方案2】:

    请参考link 以了解 getContentType 的工作原理。因此,根据定义,它将返回“text/plain”,因为您之前没有设置该值。使用以下代码行设置内容类型值。

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

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      MimeMessageHelper 的 .setText() 方法接受 2 个参数:文本本身,如果文本是 html,则为布尔值。

      因此,需要换行

      helper.setText(text);
      

      helper.setText(html, true);
      

      在您的 prepareMailForExpairy 方法中。

      【讨论】:

        猜你喜欢
        • 2015-03-30
        • 2022-01-24
        • 1970-01-01
        • 2012-11-28
        • 1970-01-01
        • 1970-01-01
        • 2020-01-14
        • 1970-01-01
        • 2020-09-18
        相关资源
        最近更新 更多