1

I have a simple email program written in Java and Spring with Velocity template to format data that sends email which should be displayed in html format. However, it just displays the data along with the html tags embedded in it without actually parsing them.

Am I missing anything here?

EmailService.java

public class EmailService {
    private static final Log LOG = LogFactory.getLog(EmailService.class);
    private static final String EMAIL_SUBJECT = ":: Risk Assessment Job Summary Results::";
    private final MailSender mailSender;
    private final String emailRecipientAddress;
    private final String emailSenderAddress;
    private static final String ERROR_MSG = "Error while sending notification email";
    private final TemplateFactory velocityTemplateFactory;
    private Template riskAssessmentJobResultSummaryTemplate;

    public EmailService(MailSender mailSender, String emailRecipientAddress,
            String emailSenderAddress, TemplateFactory velocityTemplateFactory) {
        this.mailSender = mailSender;
        this.emailRecipientAddress = emailRecipientAddress;
        this.emailSenderAddress = emailSenderAddress;
        this.velocityTemplateFactory = velocityTemplateFactory;
    }

    public void notify(String messageBody) {
        JavaMailSender javaMailSender = (JavaMailSender)mailSender;
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message);
        try {
            mimeMessageHelper.setSubject(getHostname() + " - " + EMAIL_SUBJECT);
            mimeMessageHelper.setTo(emailRecipientAddress);
            mimeMessageHelper.setFrom(emailSenderAddress);
            mimeMessageHelper.setText(getEmailContent(messageBody));
            javaMailSender.send(message);
        } catch (MailException e) {
            throw new EmailSendException(ERROR_MSG, e);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    private static String getHostname() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            return "unknown host";
        }
    }

    private String getEmailContent(String messageBody) {
        try {
            StringWriter stringWriter = new StringWriter();
            this.riskAssessmentJobResultSummaryTemplate =
                    velocityTemplateFactory.create("velocity/risk-assessment-job-results-summary.vm");
            VelocityContext velocityContext = new VelocityContext();
            velocityContext.put("hostname", getHostname());
            velocityContext.put("messageBody", messageBody);
            this.riskAssessmentJobResultSummaryTemplate.merge(velocityContext, stringWriter);
            return stringWriter.toString();
        } catch (IOException ex) {
            LOG.error("Unable to read the email template", ex);
            return null;
        }
    }
}

risk-assessment-job-results-summary.vm

<h3>Summary report for Risk Assessment Job on ${hostname}:</h3>
<table style="border:none;">
    <tr><td>${messageBody}</td></tr>
</table>

Actual Email Output:

<h3>Summary report for Risk Assessment Job on KOP-DBT0J12:</h3> 
<table style="border:none;">
<tr><td>
TotalTxPending: 0
TotalTxAccepted: 0
TotalTxRejected: 0
TotalTxProcessed: 0
</td></tr>
</table>

Expected Email Output:

Summary report for Risk Assessment Job on KOP-DBT0J12:
TotalTxPending: 0
TotalTxAccepted: 0
TotalTxRejected: 0
TotalTxProcessed: 0
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Nital
  • 5,784
  • 26
  • 103
  • 195
  • Have you tried to send that string to email? as i think your program works very fine and that string is pretty good to send email – Pervez Jul 12 '17 at 18:38
  • Email sending works ! The problem is the content in that email – Nital Jul 12 '17 at 18:41

3 Answers3

2

Set the content as html

 message.setContent(getEmailContent(messageBody), "text/html; charset=utf-8")
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
2

Found the smoking gun :)

This one line change fixed the problem. Here the true flag denotes the content type is html in the email body.

mimeMessageHelper.setText(getEmailContent(messageBody), true);
Nital
  • 5,784
  • 26
  • 103
  • 195
-1

this should be the answer

mimeMessageHelper.setContent(someHtmlMessage, "text/html; charset=utf-8");
bajji
  • 1,271
  • 3
  • 15
  • 37