Java Mail API setContent() not written in the mail body as HTML

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I need to add some HTML contents into mail body. this is what i have tried so far.



 message.setContent(
"<h1>You Have a Promotion</h1>",
"text/html");

message.setContent(
"<h3>Your First Name :</h3>" + FirstNm,
"text/html");

message.setContent(
"<h3>Your Last Name :</h3>" + LastNm,
"text/html");

message.setContent(
"<h5>Your Employee ID :</h5>" + Employeeid,
"text/html");


If i get the Out put only the last field display in the body of mail which is Employee ID. i want to display all three fields in the Body of the mail.
Thank you.







share|improve this question




















  • I'm not common with the functionality you use but here is what I'm seeing. You are setting the "content" of the message several times so each time it replaces the old value.
    – dbl
    Aug 28 at 7:36















up vote
6
down vote

favorite












I need to add some HTML contents into mail body. this is what i have tried so far.



 message.setContent(
"<h1>You Have a Promotion</h1>",
"text/html");

message.setContent(
"<h3>Your First Name :</h3>" + FirstNm,
"text/html");

message.setContent(
"<h3>Your Last Name :</h3>" + LastNm,
"text/html");

message.setContent(
"<h5>Your Employee ID :</h5>" + Employeeid,
"text/html");


If i get the Out put only the last field display in the body of mail which is Employee ID. i want to display all three fields in the Body of the mail.
Thank you.







share|improve this question




















  • I'm not common with the functionality you use but here is what I'm seeing. You are setting the "content" of the message several times so each time it replaces the old value.
    – dbl
    Aug 28 at 7:36













up vote
6
down vote

favorite









up vote
6
down vote

favorite











I need to add some HTML contents into mail body. this is what i have tried so far.



 message.setContent(
"<h1>You Have a Promotion</h1>",
"text/html");

message.setContent(
"<h3>Your First Name :</h3>" + FirstNm,
"text/html");

message.setContent(
"<h3>Your Last Name :</h3>" + LastNm,
"text/html");

message.setContent(
"<h5>Your Employee ID :</h5>" + Employeeid,
"text/html");


If i get the Out put only the last field display in the body of mail which is Employee ID. i want to display all three fields in the Body of the mail.
Thank you.







share|improve this question












I need to add some HTML contents into mail body. this is what i have tried so far.



 message.setContent(
"<h1>You Have a Promotion</h1>",
"text/html");

message.setContent(
"<h3>Your First Name :</h3>" + FirstNm,
"text/html");

message.setContent(
"<h3>Your Last Name :</h3>" + LastNm,
"text/html");

message.setContent(
"<h5>Your Employee ID :</h5>" + Employeeid,
"text/html");


If i get the Out put only the last field display in the body of mail which is Employee ID. i want to display all three fields in the Body of the mail.
Thank you.









share|improve this question











share|improve this question




share|improve this question










asked Aug 28 at 7:31









Anushka lakmal

496




496











  • I'm not common with the functionality you use but here is what I'm seeing. You are setting the "content" of the message several times so each time it replaces the old value.
    – dbl
    Aug 28 at 7:36

















  • I'm not common with the functionality you use but here is what I'm seeing. You are setting the "content" of the message several times so each time it replaces the old value.
    – dbl
    Aug 28 at 7:36
















I'm not common with the functionality you use but here is what I'm seeing. You are setting the "content" of the message several times so each time it replaces the old value.
– dbl
Aug 28 at 7:36





I'm not common with the functionality you use but here is what I'm seeing. You are setting the "content" of the message several times so each time it replaces the old value.
– dbl
Aug 28 at 7:36













1 Answer
1






active

oldest

votes

















up vote
7
down vote



accepted










set the content of the method only once if it is invoked multiple times it will override the previous values.



Try this :-



message.setContent(
"<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,
"text/html");


Below is code for setting text in case of multipart message



BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setContent("<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,"text/html");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("");//add file path
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("");//file name to be displayed
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);





share|improve this answer






















  • I would use a StringBuilder for content concatenation, but otherwise this is the answer.
    – deHaar
    Aug 28 at 7:41










  • but problem i facing here is h have to attach a file from a directory too. so that how can i call all the above elements and the multipart from the setContent().
    – Anushka lakmal
    Aug 28 at 8:47










  • For this set the text of the multipart let me edit my answer
    – vinay chhabra
    Aug 28 at 8:52










  • can i give my whole code segment?
    – Anushka lakmal
    Aug 28 at 8:53






  • 1




    Thank you very much Sir. final code segment help me a lot.
    – Anushka lakmal
    Aug 28 at 15:06










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',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
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%2f52052404%2fjava-mail-api-setcontent-not-written-in-the-mail-body-as-html%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
down vote



accepted










set the content of the method only once if it is invoked multiple times it will override the previous values.



Try this :-



message.setContent(
"<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,
"text/html");


Below is code for setting text in case of multipart message



BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setContent("<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,"text/html");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("");//add file path
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("");//file name to be displayed
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);





share|improve this answer






















  • I would use a StringBuilder for content concatenation, but otherwise this is the answer.
    – deHaar
    Aug 28 at 7:41










  • but problem i facing here is h have to attach a file from a directory too. so that how can i call all the above elements and the multipart from the setContent().
    – Anushka lakmal
    Aug 28 at 8:47










  • For this set the text of the multipart let me edit my answer
    – vinay chhabra
    Aug 28 at 8:52










  • can i give my whole code segment?
    – Anushka lakmal
    Aug 28 at 8:53






  • 1




    Thank you very much Sir. final code segment help me a lot.
    – Anushka lakmal
    Aug 28 at 15:06














up vote
7
down vote



accepted










set the content of the method only once if it is invoked multiple times it will override the previous values.



Try this :-



message.setContent(
"<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,
"text/html");


Below is code for setting text in case of multipart message



BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setContent("<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,"text/html");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("");//add file path
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("");//file name to be displayed
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);





share|improve this answer






















  • I would use a StringBuilder for content concatenation, but otherwise this is the answer.
    – deHaar
    Aug 28 at 7:41










  • but problem i facing here is h have to attach a file from a directory too. so that how can i call all the above elements and the multipart from the setContent().
    – Anushka lakmal
    Aug 28 at 8:47










  • For this set the text of the multipart let me edit my answer
    – vinay chhabra
    Aug 28 at 8:52










  • can i give my whole code segment?
    – Anushka lakmal
    Aug 28 at 8:53






  • 1




    Thank you very much Sir. final code segment help me a lot.
    – Anushka lakmal
    Aug 28 at 15:06












up vote
7
down vote



accepted







up vote
7
down vote



accepted






set the content of the method only once if it is invoked multiple times it will override the previous values.



Try this :-



message.setContent(
"<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,
"text/html");


Below is code for setting text in case of multipart message



BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setContent("<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,"text/html");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("");//add file path
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("");//file name to be displayed
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);





share|improve this answer














set the content of the method only once if it is invoked multiple times it will override the previous values.



Try this :-



message.setContent(
"<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,
"text/html");


Below is code for setting text in case of multipart message



BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setContent("<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm +
"<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid ,"text/html");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("");//add file path
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("");//file name to be displayed
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 28 at 9:36

























answered Aug 28 at 7:36









vinay chhabra

50427




50427











  • I would use a StringBuilder for content concatenation, but otherwise this is the answer.
    – deHaar
    Aug 28 at 7:41










  • but problem i facing here is h have to attach a file from a directory too. so that how can i call all the above elements and the multipart from the setContent().
    – Anushka lakmal
    Aug 28 at 8:47










  • For this set the text of the multipart let me edit my answer
    – vinay chhabra
    Aug 28 at 8:52










  • can i give my whole code segment?
    – Anushka lakmal
    Aug 28 at 8:53






  • 1




    Thank you very much Sir. final code segment help me a lot.
    – Anushka lakmal
    Aug 28 at 15:06
















  • I would use a StringBuilder for content concatenation, but otherwise this is the answer.
    – deHaar
    Aug 28 at 7:41










  • but problem i facing here is h have to attach a file from a directory too. so that how can i call all the above elements and the multipart from the setContent().
    – Anushka lakmal
    Aug 28 at 8:47










  • For this set the text of the multipart let me edit my answer
    – vinay chhabra
    Aug 28 at 8:52










  • can i give my whole code segment?
    – Anushka lakmal
    Aug 28 at 8:53






  • 1




    Thank you very much Sir. final code segment help me a lot.
    – Anushka lakmal
    Aug 28 at 15:06















I would use a StringBuilder for content concatenation, but otherwise this is the answer.
– deHaar
Aug 28 at 7:41




I would use a StringBuilder for content concatenation, but otherwise this is the answer.
– deHaar
Aug 28 at 7:41












but problem i facing here is h have to attach a file from a directory too. so that how can i call all the above elements and the multipart from the setContent().
– Anushka lakmal
Aug 28 at 8:47




but problem i facing here is h have to attach a file from a directory too. so that how can i call all the above elements and the multipart from the setContent().
– Anushka lakmal
Aug 28 at 8:47












For this set the text of the multipart let me edit my answer
– vinay chhabra
Aug 28 at 8:52




For this set the text of the multipart let me edit my answer
– vinay chhabra
Aug 28 at 8:52












can i give my whole code segment?
– Anushka lakmal
Aug 28 at 8:53




can i give my whole code segment?
– Anushka lakmal
Aug 28 at 8:53




1




1




Thank you very much Sir. final code segment help me a lot.
– Anushka lakmal
Aug 28 at 15:06




Thank you very much Sir. final code segment help me a lot.
– Anushka lakmal
Aug 28 at 15:06

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52052404%2fjava-mail-api-setcontent-not-written-in-the-mail-body-as-html%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery