Solved

When can I delete the physical file when using javax.mail MimeBodyPart

Posted on 2004-08-25
18
505 Views
Last Modified: 2008-01-16
Hi!
I'm using the MimeBodyPart to add files as attachments.
The point is that I want to delete the files before sending the message.
Can you let me know if and when I can delete it?
The code that I'm talking about (from the javax.mail example) is:

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

Thanks!
0
Comment
Question by:rzvika2
  • 7
  • 6
  • 5
18 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11899722
after you have sent it I'd say.
0
 

Author Comment

by:rzvika2
ID: 11899724
No, I need a way to delete it before sending.
0
 
LVL 92

Expert Comment

by:objects
ID: 11899739
Then read it into memory, and use a memory based DataSource.
0
 

Author Comment

by:rzvika2
ID: 11899744
Can you give me an example please?
0
 
LVL 92

Expert Comment

by:objects
ID: 11899772
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11900151
there's an example of ByteArrayDataSource in the examples that come with javamail...
0
 

Author Comment

by:rzvika2
ID: 11900298
Thanks, I have another related issue,
I need to convert my Multipart to a byte array and the easiest thing is using serialization.
I can't do it because the Multipart is not serializable.
What is your best solution?
Thanks!
0
 
LVL 92

Accepted Solution

by:
objects earned 90 total points
ID: 11900329
ByteArrayOutputStream out = new ByteArrayOutputStream();
part.writeTo(out);
out.close();
byte[] bytes = out.toByteArray();
0
 

Author Comment

by:rzvika2
ID: 11900364
I have class A that has String, and I have class B that extends A and has Integer and Multipart.
I need 2 methods in class B, one to load the object according to byte[] and one to save to byte[].
The easiest way was to do:
on save:
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ObjectOutputStream out = new ObjectOutputStream(bos);
      out.writeObject(this);
      out.close();
      return bos.toByteArray();

on load:
      ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
      ObjectInputStream in = new ObjectInputStream(bis);

Can you please answer again to my last question
(sorry for the bothering... :-) )
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:TimYates
ID: 11900410
3 questions in one? ;-)

What's the question for the last part?

Why are you trying to serialize the Multipart?
0
 

Author Comment

by:rzvika2
ID: 11900429
Yes, one thing leads to another ;-) (but I raise the points each time...)
The actual question deals with saving and loading an object to byte[], where not all the objects that it contains, are serialized.
0
 
LVL 92

Expert Comment

by:objects
ID: 11900448
use the code i posted above to serialize your multipart.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11900450
I don't think you can...  That's the point of the Serializable interface, it stops you serializing things that cannot be serialized...

So, you can save the data by using writeTo() as objects showed you, but not load it back in...

Again, why are you serializing the Multipart object?  Why not send the data needed to create it, to the place where it is to be used instead?
0
 

Author Comment

by:rzvika2
ID: 11900470
I must save and load object B to byte array (this is not an option).
I save it because I'm not sending it immediately and I don't want to use the files.
0
 
LVL 92

Expert Comment

by:objects
ID: 11900491
Why don't you save the message details *before* you create your Multipart.
ie. save what you need to create the multipart.

0
 
LVL 35

Expert Comment

by:TimYates
ID: 11900499
I don't believe this can be done...  I *think* that you will have to create it when you want to send (as it is not serializable)

I may be wrong though...

Hopefully objects can show you how :-)
0
 

Author Comment

by:rzvika2
ID: 11900646
Someone packs for me this multipart according to a user selection (of files).
I have to save it for a later usage.
I think the most convenient way is to work with serialized object.
Can't I just derive from MimeBodyPart and Multipart and implement them as serializable?
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 35 total points
ID: 11900667
> Can't I just derive from MimeBodyPart and Multipart and implement them as serializable?

I think you will hit a stumbling block somewhere down the line :-/  If not in MimeBodyPart, then maybe DataHandler, CommandInfo, CommandMap, or any of the may other classes tied in indirectly with those two classes...

It might work though...  But experience tells me it's probably a long and winding dead end... :-(
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unexpected HttpURLConnection connection behavior 2 84
how do i compare an object based on two fields 6 48
Cipher Configuration on Apache HTTPD 4 50
custom annotations 9 27
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now