ravi tiwari
ravi tiwari

Reputation: 531

Getting gmail attachment size programmatically before downloading

I am downloading my Gmail attachment using Multipart/Bodypart concept. The downloading is fine and working. But not able to get the attachment size before actually downloading the file.

Here is code snippet:

Store store = s.getStore("imaps");
store.connect("imap.gmail.com", "[email protected]", "password");
Folder inbox = store.getFolder("Misc");
inbox.open(Folder.READ_ONLY);
Message[] msgs = inbox.getMessages();
Multipart m=(Multipart)msgs[inbox.getMessageCount()-1].getContent(); //taking latest email
for(int i = 0;i < m.getCount(); i++){
BodyPart bp = m.getBodyPart(i);
String disposition = bp.getDisposition();
if(disposition!=null &&(disposition.equals("ATTACHMENT"))){
/*downloading code here */
}
}

How to get attachment size before actually downloading it?

Upvotes: 2

Views: 418

Answers (1)

Armaan Stranger
Armaan Stranger

Reputation: 3130

try this:

ByteArrayOutputStream os = new ByteArrayOutputStream();
                m.writeTo(os);
                int bytes = os.size();

Upvotes: 2

Related Questions