aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Finding email attachment  size using java mail api Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Finding email attachment  size using java mail api" Watch "Finding email attachment  size using java mail api" New topic
Author

Finding email attachment size using java mail api

N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
Hi i want to know how we can get exact size of an email attachment(any type of file) using java mail api.
This is very crucial for me in my project. Please help me.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Its better to use addBodyPart(BodyPart part, int index), while attaching the bodyPart which has file attached. Then you can get that bodyPart using mimeMultiPart.getBodyPart(int index). And Part has a method getSize() to know the approximate size of the body part content.

To be more exact you might want to encode your file using base64 encoding - assuming this transfer encoding will be used - and then find out the size of that encoded file.
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
i just want to know the size of attachment file not entire bodypart.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
The bodyPart, I am talking about, should have just the attachment not anything else. Use different body parts for actual message and for attachment. I hope I made my point clear enough. x attachements, x+1 bodyParts, where 1 is for the actual message.
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
we are also using other email clients like thunderbird,outlook express at that time it is not possible to add attachment using index, because when we use other email clients it is not calling our application code. Is it possible to get the size of attachment using datahandler and datasource object. how we can encrypt a file to get the size?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

If you have a DataHandler, it's quite easy - just read the InputStream and count the bytes. Or alternatively (but using the same approach), create a special OutputStream and use the writeTo method:
Although reading the InputStream directly is probably more efficient; you won't need that extra class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
Thanks "Rob Prime" that code solved my problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding email attachment size using java mail api