Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding email attachment size using java mail api

 
N Naresh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just want to know the size of attachment file not entire bodypart.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
N Naresh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks "Rob Prime" that code solved my problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic