Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setting content in email with attachment JavaMail

 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to send email with and without attachments...however when i send an email with an attachment i cannot set anything in the content of the email...

this is the line that with without attachment works
message.setContent("This is a test.", "text/plain");

but when i have an attachment i enable this line and it does not set that content to the email
attachFilePart.setText("This is a test from attachment");

and havent had any succes....ANY IDEAS??
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a message part for the content.
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean this....

message.setContent("This is a test.", "text/plain");
 
Paul Clapham
Sheriff
Pie
Posts: 20754
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You have a Multipart. Its name implies it will have more than one part, right? You created a Part for the attachment and added it to the Multipart. Now do the same thing for the text.
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you mean this...

message.setText("This is a test from attachment.");

However when i do this, the same thing happens...the attachment does not gets send, just the content that line of text....its is either or, how is this possible?
 
Paul Clapham
Sheriff
Pie
Posts: 20754
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what you said before. And then I said "No". So that should lead you to believe that I do NOT mean that. Or did you not read my post?

I also suggested what you should do instead. I assumed that since you had code which created one part object, you would not find it difficult to write more code which created a second part object. What trouble are you having with that?
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since you are very smart, from my post dont it is obvious understand exactly how it works even though ive tried different options, or did you not read my post?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me add some things together:
David Newton wrote:Create a message part for the content.

Paul Clapham wrote:No. You have a Multipart. Its name implies it will have more than one part, right? You created a Part for the attachment and added it to the Multipart. Now do the same thing for the text.

You have a Multipart. You add a BodyPart to it for the file. Now follow David's advice: create a BodyPart for the text. Then follow Paul's advice: add that BodyPart to the Multipart.

So don't call message.setText - that will throw away the Multipart. Call setText on a newly created BodyPart instead.
 
Eduardo Ponce de Leon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thans rob......Paul really didnt explained himself that good... now I understand what needs to be done.. as simple as a CREATE a MimeBodyPart for the text, then another for the attachement, then add them to the Multipart and set the multipart to the message!!


 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eduardo Ponce de Leon wrote:thans rob......Paul really didnt explained himself that good... now I understand what needs to be done.. as simple as a CREATE a MimeBodyPart for the text, then another for the attachement, then add them to the Multipart and set the multipart to the message!!

I may not read that well, but I did get exactly that from Paul's first post:
Paul Clapham wrote:You created a Part for the attachment and added it to the Multipart. Now do the same thing for the text.

That "do the same thing" would have me create a Part for the text similar to how you created a Part for the attachment (MimeBodyPart), then add it to the Multipart. that only leaves the setting of the content of the new (Body)Part but the API would have let me use either setContent or setText.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic