Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does anyone know how to download attachment?? #4

Open
GoogleCodeExporter opened this issue Dec 1, 2015 · 4 comments
Open

Does anyone know how to download attachment?? #4

GoogleCodeExporter opened this issue Dec 1, 2015 · 4 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 27 Mar 2010 at 5:08

@GoogleCodeExporter
Copy link
Author

Sorry..wrong typing.
i mean: anyone help??

Original comment by [email protected] on 27 Mar 2010 at 5:11

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 31 Mar 2010 at 1:17

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 31 Mar 2010 at 3:00

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 31 Mar 2010 at 3:37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant