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.
-
Have you tried: message.setContent(msg, "text/html; charset=utf-8");Luca Sepe– Luca Sepe2014年03月19日 20:37:04 +00:00Commented 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.André– André2014年03月19日 20:43:34 +00:00Commented Mar 19, 2014 at 20:43
-
Have you seen the advice on the Oracle site?Bobulous– Bobulous2014年03月19日 20:56:04 +00:00Commented 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 workPaul Taylor– Paul Taylor2014年03月19日 21:21:34 +00:00Commented Mar 19, 2014 at 21:21
1 Answer 1
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!
-
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");Fabian Rivera– Fabian Rivera2014年03月19日 21:26:33 +00:00Commented 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!Fabian Rivera– Fabian Rivera2014年03月19日 21:29:07 +00:00Commented 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.Bill Shannon– Bill Shannon2014年03月19日 23:44:19 +00:00Commented 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.Paul Taylor– Paul Taylor2014年03月20日 09:47:27 +00:00Commented Mar 20, 2014 at 9:47