Java Mail API setContent() not written in the mail body as HTML
Clash 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.
java javamail
add a comment |Â
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.
java javamail
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
add a comment |Â
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.
java javamail
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.
java javamail
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
add a comment |Â
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
add a comment |Â
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);
I would use aStringBuilder
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
 |Â
show 3 more comments
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);
I would use aStringBuilder
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
 |Â
show 3 more comments
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);
I would use aStringBuilder
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
 |Â
show 3 more comments
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);
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);
edited Aug 28 at 9:36
answered Aug 28 at 7:36
vinay chhabra
50427
50427
I would use aStringBuilder
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
 |Â
show 3 more comments
I would use aStringBuilder
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
 |Â
show 3 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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