简体   繁体   中英

HTML is not rendering on email template, coming as text

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.

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");

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM