aspose file tools*
The moose likes Java in General and the fly likes setting content in email with attachment JavaMail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "setting content in email with attachment JavaMail" Watch "setting content in email with attachment JavaMail" New topic
Author

setting content in email with attachment JavaMail

Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
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

Joined: Sep 29, 2008
Posts: 12617

Create a message part for the content.
Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
you mean this....

message.setContent("This is a test.", "text/plain");
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

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

Joined: May 13, 2009
Posts: 99
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
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

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

Joined: May 13, 2009
Posts: 99
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting content in email with attachment JavaMail