• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding email attachment size using java mail api

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks "Rob Prime" that code solved my problem.
reply
    Bookmark Topic Watch Topic
  • New Topic