HTML未在电子邮件模板上呈现,以文本形式呈现

时间:2020-05-04 14:54:38

标签: java html spring-boot mail-server

我已经编写了用于发送电子邮件通知的代码,电子邮件已正确发送到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可以呈现,我还必须添加其他代码。

3 个答案:

答案 0 :(得分:0)

您可以跳过设置traceback (most recent call last): File "setup.py", line 2, in <module> 的部分。使用MimeBodyPart直接设置您的内容以及内容类型。这对我有用:

Mimemessage

答案 1 :(得分:0)

请参阅此link以了解getContentType的工作方式。因此根据定义,由于您之前未设置该值,因此它将返回“文本/纯文本”。使用以下代码行设置内容类型值。

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

希望这会有所帮助。

答案 2 :(得分:0)

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

因此,您需要更改行

helper.setText(text);

helper.setText(html, true);

在prepareMailForExpairy方法中。

相关问题