0

Hopefully just a quick one.

Essentially I am using apache commons email v1.3.3 and I am trying to send a HTML format email. I have followed their user guide to do this however the email I am receiving is just not resolving to HTML in any client I view it in, all of which support HTML..

Here is the snippet of code essentially sending it:

HtmlEmail email = new HtmlEmail();
    email.setSubject(subject);
    email.setTo(getRecipients(recipients));
    email.setHtmlMsg(htmlMsg);
    email.setTextMsg(alternativeMsg);
    try {
        this.mailServer.send(email);
    }
    catch (EmailException e) {
        LOGGER.error("An error occurred sending email. ", e);
    }

Now lets say the html is this:

<html>some text in html <p> blah blah blah </html>

I am just receiving plain text content as is above.

Could someone please highlight what I am missing?

Thanks,

Edit:

Having made use of the debug feature, I can see the content-type remains plain/text. To resolve my issue I have instead done this:

email.setContent(htmlMsg, EmailConstants.TEXT_HTML);

dapperwaterbuffalo
  • 2,672
  • 5
  • 35
  • 48

1 Answers1

0

I have been using the commons-email library for quite some time. I think, you set the message using the setMsg() method instead of setHtmlMsg(). Rewrite the code as below, and it should work fine.

HtmlEmail email = new HtmlEmail();
    email.setSubject(subject);
    email.setTo(getRecipients(recipients));
    email.setMsg(htmlMsg);

    try {
        this.mailServer.send(email);
    }
    catch (EmailException e) {
        LOGGER.error("An error occurred sending email. ", e);
    }

There is another problem that I faced with this HTML email. The library adds a <pre> tag to our message, and that makes the styles and alignments a bit odd. I had to override the setMsg() to overcome this problem.

Kris
  • 8,680
  • 4
  • 39
  • 67