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.
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.
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.
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?
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.