6

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.

asked Mar 19, 2014 at 20:32
4
  • Have you tried: message.setContent(msg, "text/html; charset=utf-8"); Commented Mar 19, 2014 at 20:37
  • you can also use message.addPart(msg,"text/html;charset=UTF-8"); As long is within <html></html> tags it works. Commented Mar 19, 2014 at 20:43
  • Have you seen the advice on the Oracle site? Commented Mar 19, 2014 at 20:56
  • Yes I tried message.setContent(msg, "text/html; charset=utf-8"); first and it didn't work to mak esure Ive tried it again and it still doesnt work Commented Mar 19, 2014 at 21:21

1 Answer 1

8

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!

answered Mar 19, 2014 at 20:41
4
  • First you're calling setContent method, and then setText. I think this is the problem. You only need one method as I wrote in my post, try avoiding your setText method and keep message.setContent(msg, "text/html; charset=utf-8"); Commented Mar 19, 2014 at 21:26
  • I have edited my post, try something like this. I am not testing it, but I think it should be in that way. Hope it works for you! Commented Mar 19, 2014 at 21:29
  • setText overrides what setContent does. Just use the setText method that allows you to specify the charset and text type. That lets JavaMail format the Content-Type header for you, taking care of quoting, etc. Commented Mar 19, 2014 at 23:44
  • Doh, thanks guys until @BillShannon comment I was totally failing to see that I had the setText() method in there as well as setContent(), removing setText() fixed the issue. Commented Mar 20, 2014 at 9:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.