Ive been sending plaintest email from Java no problem but Im now trying to send a html one as follows:
MimeMessage message = new MimeMessage(Email.getSession());
message.setFrom(new InternetAddress("[email protected]"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to, true));
message.setSubject(subject);
message.setContent(msg, "text/html");
message.setText(msg);
message.saveChanges();
Transport.send(message);
However when I receive it in my client it receives it as a plain text email, i.e it shows all the html tags instead of them being used for formatting, and I have checked the email header and it does say
Content-Type: text/plain; charset=us-ascii
in the mail header
but why because I pass "text/html" to the setContent() method and that seems to be the only thing you have to do.
The most important thing to know about HTML email is that you can't just attach an HTML file and a bunch of images to a message, then click send. Most of the time, your recipient's email application will break all the paths to your image files by moving your images into temporary folders on the recipient's hard drive.
You can try the following:
message.setText(msg, "utf-8", "html");
or
message.setContent(msg, "text/html; charset=utf-8");
Avoid the setText method, you only need setContent.
It should be like this:
MimeMessage message = new MimeMessage(Email.getSession());
message.setFrom(new InternetAddress("[email protected]"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to, true));
message.setSubject(subject);
message.setContent(msg, "text/html; charset=utf-8");
message.saveChanges();
Transport.send(message);
Hope it helps you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With