samkazmi / javamail-android

Automatically exported from code.google.com/p/javamail-android
0 stars 0 forks source link

Does anyone know how to download attachment?? #4

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Does anyone know how to download attachment??
I can use pop3 to download the whole email...
but how to separate the email content and attachment?? 
anyone hell??

Original issue reported on code.google.com by 36m...@gmail.com on 27 Mar 2010 at 5:08

GoogleCodeExporter commented 9 years ago
Sorry..wrong typing.
i mean: anyone help??

Original comment by 36m...@gmail.com on 27 Mar 2010 at 5:11

GoogleCodeExporter commented 9 years ago
Here is my code for saving the attachment, but it is IMAP Based, don't know if 
it's 
different for POP3.  I got the original example from a Sun Javamail example.

String folder = getAttachmentFolderForMessage(getMessageUID(msg));      

if(msg.isMimeType("multipart/*"))
{   //It's a a multipart so go through the parts and find what looks like the 
attachments
    Multipart mp = (Multipart)msg.getContent();
    int partCount = mp.getCount();
    Part curtPart;
    String curPartDisposition;
    String filename;
    File file;
    for (int i = 0; i < partCount; i++)
    {
    curtPart = mp.getBodyPart(1);
    curPartDisposition = curtPart.getDisposition();
        // many mailers don't include a Content-Disposition
        if (curPartDisposition == null || curPartDisposition.equalsIgnoreCase
(Part.ATTACHMENT)) 
        {
        filename = curtPart.getFileName();
            if (filename == null || filename.length() == 0)
                filename = "Attachment" + i;
            filename = folder + filename;
            try 
            {
                file = new File(filename);
                ((MimeBodyPart)curtPart).saveFile(file);
                Log.d(LogTag,"Saved the attachment "+i+" to the 
following filename ["+filename+"].");
            } 
            catch (IOException ex) 
            {
                Log.e(LogTag,"Caught an exception trying to save an 
attachment to the filename ["+filename+"].",ex);
                throw new IMAPException("Unable to save the attachment 
to a file. "+ex.getMessage());
            }
        }
    }
}

Original comment by j...@ahg.net on 31 Mar 2010 at 1:17

GoogleCodeExporter commented 9 years ago
Thank you for your response..
but the statement: Multipart mp = (Multipart)msg.getContent(); 
cannot work on this javamail-android's jar, 
it will throws exceptions: 
SharedByteArrayInputStream cannot be cast to javax.mail.Multipart

Original comment by 36m...@gmail.com on 31 Mar 2010 at 3:00

GoogleCodeExporter commented 9 years ago
I don't know the full answer but from what I see in the comments and the code 
this 
is a difference in the mail message you are getting from your mail server.  It 
might 
also have to do with POP3 over IMAP, cause the above code defentily does work 
for 
me.  I would check out the SharedByteArrayInputStream and see if you can read 
from 
it to get the attachment.  Maybe also check the Sun site for javamail and 
checkout 
the FAQ and or the demo's, they have some good code examples.

Original comment by j...@ahg.net on 31 Mar 2010 at 3:37