How to send html message using java mail

I sent the complaint letter with Java without problems, but Im now trying to send html one of them as follows:

        MimeMessage message = new MimeMessage(Email.getSession());
        message.setFrom(new InternetAddress("support@jthink.net"));
        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 text email, i.e. it displays all html tags instead of being used for formatting, and I checked the email header and it says

Content-Type: text/plain; charset=us-ascii

in the mail header

but why, because I pass "text / html" to the setContent () method, and this seems to be the only thing you need to do.

+4
source share
1 answer

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.

:

MimeMessage message = new MimeMessage(Email.getSession()); 
message.setFrom(new InternetAddress("support@jthink.net"));
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);

, !

+5

Source: https://habr.com/ru/post/1532551/


All Articles