0

I am trying to send the mail using JavaMailSender which include the html content but instead of the rendered html in the email i am getting the html code itself in the mail i.e

Email which i am getting

Method used to send email

public void sendMimeMessage(String from, String to, String subject, String messageBody, String... cc) {
    MimeMessage message = mailSender.createMimeMessage()

    try {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(from)
        helper.setSentDate(new Date())
        helper.setSubject(subject)
        helper.setText(messageBody, true)
        helper.setTo(to)
        helper.setCc(cc)

        mailSender.send(message)
        log.debug("Email successfully sent to <${to}> with cc <${cc}> and with subject <${subject}> and Email body: ${messageBody}")
    } catch (Exception exception) {
        exception.printStackTrace()
        log.error("Email to <${to}> with subject <${subject}> could not be sent due to: ", exception)
    }
}

Any help would be appreciated.

Rahul Babu
  • 730
  • 1
  • 5
  • 25
  • have you tried by this way? MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo("test@host.com"); // use the true flag to indicate the text included is HTML helper.setText("", true); – KhAn SaAb Apr 07 '16 at 10:42
  • Yes, I am using the below code >> MimeMessage message = mailSender.createMimeMessage() MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from) helper.setSentDate(new Date()) helper.setSubject(subject) helper.setText(messageBody, true) helper.setTo(to) helper.setCc(cc) mailSender.send(message) – Rahul Babu Apr 07 '16 at 10:46
  • updated the question with method used to send email – Rahul Babu Apr 07 '16 at 10:50
  • Try message.setContent(messageBody,"text/html; charset="+"UTF-8"); – Unknown Apr 07 '16 at 12:57

2 Answers2

0

This might help you.

You can use Apache velicity, or can see my Answer.

Sample code:

JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");

MimeMessage message = sender.createMimeMessage();

// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo("test@host.com");

// use the true flag to indicate the text included is HTML
helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true);

// let's include the infamous windows Sample file (this time copied to c:/)
FileSystemResource res = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addInline("identifier1234", res);

sender.send(message);
Community
  • 1
  • 1
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
0

This worked for me.

Step 1. Construct the messageBody as follows:

Let suppose you need a HTML content in mail like shown below

**SERIAL NO    PROCESS_INSTANCE_ID        USONUMBER** 
1              TSV00876                   N89876    
2              LMNV0083                   V89876 

Let suppose you have list from where you are going to populate above columns.
List<Report> list = xyzService.getReport(); **//returns list of Report**

    StringBuilder buf = new StringBuilder();
            buf.append("<!doctype html>\n");
            buf.append("<html lang='en'>\n");
            buf.append("<head>\n");
            buf.append("<meta charset='utf-8'>\n");
            buf.append("<title>Report</title>\n");
            buf.append("</head>\n");
            buf.append("<body>" +
                       "<table border ='1'>" + "<tr>" + 
                       "<th>SERIAL NO</th>" +
                       "<th>PROCESS_INSTANCE_ID</th>" + 
                       "<th>USONUMBER</th>"+
                       "</tr>\n");
    
              for (int i = 0; i < list.size(); i++) {
                buf.append("<tr><td>").
                append(list.get(i)).getSerialNo().append("</td><td>").
                append(list.get(i).getProcessInstanceId()).append("</td><td>").
                append(list.get(i).getUsoNumber()).append("</td><td>").
                append("</td></tr>\n");
            }
            buf.append("</table>\n" + 
                       "</body>\n" + 
                       "</html>");
            String messageBody = buf.toString(); 

Step 2. pass this string messageBody in your method sendMimeMessage() and set MimeMessageHelper as shown below.

MimeMessage message = mailSender.createMimeMessage()
MimeMessageHelper helper = new MimeMessageHelper(message,MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,StandardCharsets.UTF_8.name());
                helper.setText(messageBody, true);
Abdullah Imran
  • 259
  • 3
  • 13