JavaMail - Send plain HTML content without formatting












0















I want to send an email using JavaMail with HTML tags not formatted.



I use this code :



public void sendMail() throws MessagingException, IllegalStateException, IllegalArgumentException {

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "localhost");

Session session = Session.getDefaultInstance(properties);

MimeMessage message = new MimeMessage(session);

MimeMultipart multipart = new MimeMultipart();

MimeBodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setContent("<h1>This is an HTML message</h1>", "text/plain; charset=UTF-8");

multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

message.setFrom(new InternetAddress("noreply@localhost.com"));

message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@localhost.com"));

message.setSubject("HTML mail");

Transport.send(message);
}


I receive the email with formatted HTML :




This is an HTML message




But I want html plain text :




< h1>This is an HTML message< /h1>




Thanks for help !










share|improve this question























  • What you have should work. Possibly your mail reader is guessing that you really meant text/html even though you said text/plain because the text looks like html text?

    – Bill Shannon
    Nov 16 '18 at 19:01
















0















I want to send an email using JavaMail with HTML tags not formatted.



I use this code :



public void sendMail() throws MessagingException, IllegalStateException, IllegalArgumentException {

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "localhost");

Session session = Session.getDefaultInstance(properties);

MimeMessage message = new MimeMessage(session);

MimeMultipart multipart = new MimeMultipart();

MimeBodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setContent("<h1>This is an HTML message</h1>", "text/plain; charset=UTF-8");

multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

message.setFrom(new InternetAddress("noreply@localhost.com"));

message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@localhost.com"));

message.setSubject("HTML mail");

Transport.send(message);
}


I receive the email with formatted HTML :




This is an HTML message




But I want html plain text :




< h1>This is an HTML message< /h1>




Thanks for help !










share|improve this question























  • What you have should work. Possibly your mail reader is guessing that you really meant text/html even though you said text/plain because the text looks like html text?

    – Bill Shannon
    Nov 16 '18 at 19:01














0












0








0








I want to send an email using JavaMail with HTML tags not formatted.



I use this code :



public void sendMail() throws MessagingException, IllegalStateException, IllegalArgumentException {

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "localhost");

Session session = Session.getDefaultInstance(properties);

MimeMessage message = new MimeMessage(session);

MimeMultipart multipart = new MimeMultipart();

MimeBodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setContent("<h1>This is an HTML message</h1>", "text/plain; charset=UTF-8");

multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

message.setFrom(new InternetAddress("noreply@localhost.com"));

message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@localhost.com"));

message.setSubject("HTML mail");

Transport.send(message);
}


I receive the email with formatted HTML :




This is an HTML message




But I want html plain text :




< h1>This is an HTML message< /h1>




Thanks for help !










share|improve this question














I want to send an email using JavaMail with HTML tags not formatted.



I use this code :



public void sendMail() throws MessagingException, IllegalStateException, IllegalArgumentException {

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "localhost");

Session session = Session.getDefaultInstance(properties);

MimeMessage message = new MimeMessage(session);

MimeMultipart multipart = new MimeMultipart();

MimeBodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setContent("<h1>This is an HTML message</h1>", "text/plain; charset=UTF-8");

multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

message.setFrom(new InternetAddress("noreply@localhost.com"));

message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@localhost.com"));

message.setSubject("HTML mail");

Transport.send(message);
}


I receive the email with formatted HTML :




This is an HTML message




But I want html plain text :




< h1>This is an HTML message< /h1>




Thanks for help !







html javamail mime-types content-type






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 8:27









azbazb

32




32













  • What you have should work. Possibly your mail reader is guessing that you really meant text/html even though you said text/plain because the text looks like html text?

    – Bill Shannon
    Nov 16 '18 at 19:01



















  • What you have should work. Possibly your mail reader is guessing that you really meant text/html even though you said text/plain because the text looks like html text?

    – Bill Shannon
    Nov 16 '18 at 19:01

















What you have should work. Possibly your mail reader is guessing that you really meant text/html even though you said text/plain because the text looks like html text?

– Bill Shannon
Nov 16 '18 at 19:01





What you have should work. Possibly your mail reader is guessing that you really meant text/html even though you said text/plain because the text looks like html text?

– Bill Shannon
Nov 16 '18 at 19:01












1 Answer
1






active

oldest

votes


















0














Use message.setText() instead of message.setContent()






share|improve this answer



















  • 1





    This should have been a comment but you have not enough reputation for this.

    – Rarblack
    Nov 16 '18 at 9:34











  • Indeed, I tried but couldn't comment.

    – Ouamefi
    Nov 16 '18 at 10:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334010%2fjavamail-send-plain-html-content-without-formatting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Use message.setText() instead of message.setContent()






share|improve this answer



















  • 1





    This should have been a comment but you have not enough reputation for this.

    – Rarblack
    Nov 16 '18 at 9:34











  • Indeed, I tried but couldn't comment.

    – Ouamefi
    Nov 16 '18 at 10:27
















0














Use message.setText() instead of message.setContent()






share|improve this answer



















  • 1





    This should have been a comment but you have not enough reputation for this.

    – Rarblack
    Nov 16 '18 at 9:34











  • Indeed, I tried but couldn't comment.

    – Ouamefi
    Nov 16 '18 at 10:27














0












0








0







Use message.setText() instead of message.setContent()






share|improve this answer













Use message.setText() instead of message.setContent()







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 9:25









OuamefiOuamefi

11




11








  • 1





    This should have been a comment but you have not enough reputation for this.

    – Rarblack
    Nov 16 '18 at 9:34











  • Indeed, I tried but couldn't comment.

    – Ouamefi
    Nov 16 '18 at 10:27














  • 1





    This should have been a comment but you have not enough reputation for this.

    – Rarblack
    Nov 16 '18 at 9:34











  • Indeed, I tried but couldn't comment.

    – Ouamefi
    Nov 16 '18 at 10:27








1




1





This should have been a comment but you have not enough reputation for this.

– Rarblack
Nov 16 '18 at 9:34





This should have been a comment but you have not enough reputation for this.

– Rarblack
Nov 16 '18 at 9:34













Indeed, I tried but couldn't comment.

– Ouamefi
Nov 16 '18 at 10:27





Indeed, I tried but couldn't comment.

– Ouamefi
Nov 16 '18 at 10:27




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334010%2fjavamail-send-plain-html-content-without-formatting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine