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