Downloading gmail attachment in android program

别等时光非礼了梦想. 提交于 2019-12-12 13:19:47

问题


I tried searching a lot and finally asking here.

I need to write a code to download attachments form my GMail. How can I do this?

Till now I am able to read/send emails. But still figuring out how to download the attachments. Any help will be appreciated.


回答1:


The attachments are not downloaded separately. They are part of a MIME multipart document. You can use the MimeMultipart class to unpack them.




回答2:


I have done this. Posting the solution so others can use:

    protected Integer doInBackground(Void... vd){
            Properties props = new Properties();
            props.setProperty("mail.store.protocol", "imaps");
            props.setProperty("mail.imaps.host", "imaps.gmail.com");
            props.setProperty("mail.imaps.port", "993");
            props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.setProperty("mail.imaps.socketFactory.fallback", "false");
            Session s = Session.getInstance(props);
            sb = new StringBuilder();
            try{
                    store = s.getStore("imaps");
                    store.connect("imap.gmail.com", "youremailid@gmail.com", "password");
                    Folder inbox = store.getFolder("Inbox"); //Any folder name
                    inbox.open(Folder.READ_ONLY);
                    msgs = inbox.getMessages();
                    m=(Multipart)msgs[inbox.getMessageCount()-1].getContent(); //Getting the newest email. Assuming it has one attachment.
                    for(int i=0;i<m.getCount();i++){
                            bp = m.getBodyPart(i);
                            disposition = bp.getDisposition();
                            if(disposition!=null && (disposition.equals("ATTACHMENT"))){
                                    fileName = bp.getFileName();
                                    base64dec = (InputStream)bp.getContent();
                                    OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+fileName);
                                    byte data[] = new byte[8192];
                                    long total = 0;
                                    while ((count = base64dec.read(data)) != -1){
                                            total += count;
                                            output.write(data, 0, count);
                                            publishProgress((int)total);
                                    }
                                    output.flush();
                                    output.close();base64dec.close();
                            }
                    }
            }catch(Exception e){
                    errorMsg += "\nError: "+e.toString();
                    Log.e("MyError:",e.toString());
            }
            return 0;
    }


来源:https://stackoverflow.com/questions/16806644/downloading-gmail-attachment-in-android-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!