2013-05-29 103 views
3

我嘗試了很多搜索並最終在這裏詢問。在Android程序中下載gmail附件

我需要編寫代碼到下載附件表格我的GMail。我怎樣才能做到這一點?

到現在爲止,我可以閱讀/發送電子郵件。但仍然搞清楚如何下載附件。任何幫助將不勝感激。

回答

0

附件不會單獨下載。它們是MIME多部分文檔的一部分。您可以使用MimeMultipart類來解壓縮它們。

0

我已經這樣做了。發佈解決方案以便其他人可以使用:

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", "[email protected]", "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; 
    } 
相關問題