Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Attachments

2006-03-02
2013-04-03
  • renanfsouza
    renanfsouza
    2006-03-02

    Hello everybody,

    I'm in trouble downloading attchments...

    Once logged, I get the whole message with: String msg_toda = gmc.fetchOriginalMail(gmt.getMatchingMessageID()); where gmc is a GMConnector and gmt is a GMThread.

    I outputted msg_toda on a text file with FileOutputStream and DataOutputStream.

    Reading the generatted file, I found this:
    "X-Attachment-Id: f_ekac8h2o"

    So... I supposed the attachment ID of the current message was f_ekac8h2o. Then I tried to download it as follows:

    InputStream arq = gmc.getAttachmentAsStream(at_id,gmt.getMatchingMessageID());
    FileOutputStream fos = new FileOutputStream("C:\\Renan\\bla.mp3");
    PrintStream p2 = new PrintStream(fos);
    while(arq.available() != 0)
      p2.print(arq.read());

    I saved it as a mp3 file because I'm sure the attached file is a mp3. But the generatted bla.mp3 is empty (has 0 bytes of size).

    So... Does anyone here knows how to solve this problem ?

    Thanx a lot,

    -Renan

     
    • Chris Saunders
      Chris Saunders
      2006-03-03

      I was just about to post a seperate message, but it might be related to this...

      I keep getting a ParsePacketException thrown when trying to get a GMResponse with connector.request(...).  I am using the latest code line (0.3.19) when I get this error, but I seem to be able to login ok as some of the text in the console matches up to items I have in my inbox. 

      So basically I'm wondering if perhaps Google has changed the API and that is why we both are having trouble accessing data.  I have just come across the g4j project and starting to use it this week so I don't know it well enough to offer much else.

      Cheers,
      Chris

       
    • SiuYing
      SiuYing
      2006-03-03

      The current g4j is outdated because change in gmail... this should be fixed in next release.

       
    • renanfsouza
      renanfsouza
      2006-03-08

      SiuYing,

      Is there any way to download an attachment on the current release ? If so, could you please teach me how ?

      Thanx a lot,

      -Renan

       
    • iuri_oob
      iuri_oob
      2006-03-08

      Hi,
      in my thread "no more info about attachments" i sy the same thing...

      Anyway, for any GMConversationEntry, the first attachment has always the id "0.1". You can downlaod the attachment using the gmail connector, passing the id of the GMConversationEntry, and "0.1" as attachment id. For me it works.

       
    • renanfsouza
      renanfsouza
      2006-03-08

      Iuri,

      Could you please show me a code snippet ?

      Thanx a lot,

      -Renan

       
    • iuri_oob
      iuri_oob
      2006-03-09

      Sure!

      GMConnector gmc = new GMConnector("user","paswd",1);
      gmc.connect();
      GMConversationEntry yourEntry;  // get it with a request
      String msgId = yourEntry.getId();
      InputStream attStream = gmc.getAttachmentAsStream("1.0", msgId);

      PS
      Sorry for my wrong post, the right id of the first attachment is "1.0", not "0.1"...

       
    • renanfsouza
      renanfsouza
      2006-03-10

      Iuri...

      I got what you said. But this "1.0" means "the first attachment of the message that has the ID msgIg", right ?

      Could you also please show me how to get the request for the GMConversationEntry ? Hehe

      Thanx a lot,

      -Renan

       
    • iuri_oob
      iuri_oob
      2006-03-10

      Eheh, ok... You didn't use very much the APIs, did you?.. ;-)

      import java.io.*;
      import java.util.*;
      import siuying.gm.*;
      import siuying.gm.structure.*;
      import org.apache.commons.httpclient.*;
      import org.apache.commons.httpclient.methods.*;

      class Entry2stream {
          public static void main(String[] args) {
              try {
                  List<GMConversationEntry> entries = new LinkedList();
                  GMConnector gmc = new GMConnector("iuri.fanti@gmail.com","nosceteipsum",1);
                  gmc.connect();
                  GMResponse gmr = gmc.request(GMConstants.GM_REQ_STANDARD, "Inbox", "0");
                  ArrayList<GMThread> threads = gmr.getGMThreads();
                  for(GMThread t: threads) {
                      ArrayList newEntries = ((GMConversation)gmc.request(GMConstants.GM_REQ_CONVERSATION, t.getMatchingMessageID(), "0")).getEntries();
                      entries.addAll(newEntries);
                  }
                 
                  Map entry2stream = new HashMap();
                 
                  for(GMConversationEntry e: entries) {
                      InputStream attStream = gmc.getAttachmentAsStream("1.0", e.getId());
                      entry2stream.put(e, attStream);
                  }
                 
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }

      I compiled the code before posting it to you. This class creates a Map with the first attachment as InputStrem for any GMConversationEntry.
      Anyway, this is the link to the APIs: http://g4j.sourceforge.net/doc/
      ;-)
      Good luck!

       
    • SiuYing
      SiuYing
      2006-03-12

      By: iuri_oob - iuri_oob
        RE: Attachments

      2006-03-11 02:46
      Eheh, ok... You didn't use very much the APIs, did you?.. ;-)

      import java.io.*;
      import java.util.*;
      import siuying.gm.*;
      import siuying.gm.structure.*;
      import org.apache.commons.httpclient.*;
      import org.apache.commons.httpclient.methods.*;

      class Entry2stream {
      public static void main(String[] args) {
      try {
      List<GMConversationEntry> entries = new LinkedList();
      GMConnector gmc = new GMConnector("iuri.fanti@gmail.com","nosceteipsum",1); 
      gmc.connect();
      GMResponse gmr = gmc.request(GMConstants.GM_REQ_STANDARD, "Inbox", "0");
      ArrayList<GMThread> threads = gmr.getGMThreads();
      for(GMThread t: threads) {
      ArrayList newEntries = ((GMConversation)gmc.request(GMConstants.GM_REQ_CONVERSATION, t.getMatchingMessageID(), "0")).getEntries();
      entries.addAll(newEntries);
      }

      Map entry2stream = new HashMap();

      for(GMConversationEntry e: entries) {
      InputStream attStream = gmc.getAttachmentAsStream("1.0", e.getId());
      entry2stream.put(e, attStream);
      }

      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      }

      I compiled the code before posting it to you. This class creates a Map with the first attachment as InputStrem for any GMConversationEntry.
      Anyway, this is the link to the APIs: http://g4j.sourceforge.net/doc/
      />;-)
      Good luck!

       
    • SiuYing
      SiuYing
      2006-03-12

      By: iuri_oob - iuri_oob
        RE: Attachments
      2006-03-11 02:46
      Eheh, ok... You didn't use very much the APIs, did you?.. ;-)

      import java.io.*;
      import java.util.*;
      import siuying.gm.*;
      import siuying.gm.structure.*;
      import org.apache.commons.httpclient.*;
      import org.apache.commons.httpclient.methods.*;

      class Entry2stream {
      public static void main(String[] args) {
      try {
      List<GMConversationEntry> entries = new LinkedList();
      GMConnector gmc = new GMConnector("user","password",1); 
      gmc.connect();
      GMResponse gmr = gmc.request(GMConstants.GM_REQ_STANDARD, "Inbox", "0");
      ArrayList<GMThread> threads = gmr.getGMThreads();
      for(GMThread t: threads) {
      ArrayList newEntries = ((GMConversation)gmc.request(GMConstants.GM_REQ_CONVERSATION, t.getMatchingMessageID(), "0")).getEntries();
      entries.addAll(newEntries);
      }

      Map entry2stream = new HashMap();

      for(GMConversationEntry e: entries) {
      InputStream attStream = gmc.getAttachmentAsStream("1.0", e.getId());
      entry2stream.put(e, attStream);
      }

      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      }

      I compiled the code before posting it to you. This class creates a Map with the first attachment as InputStrem for any GMConversationEntry.
      Anyway, this is the link to the APIs: http://g4j.sourceforge.net/doc/
      />;-)
      Good luck!

       
    • renanfsouza
      renanfsouza
      2006-03-13

      Iuri... I used your code, but I got sttuked at the same point as mine's.

      Take a look: the idea here is to get the attachment by the name:

      try {
      GMConnector gmc = new GMConnector("user","password",1); 
      gmc.connect();
      GMResponse response = gmc.request(GMConstants.GM_REQ_STANDARD,"Inbox","0");
      ArrayList msg = response.getGMThreads();
      for(int i=0;i<msg.size();i++) {
                      GMThread gmt = (GMThread) msg.get(i);
                      System.out.println(gmt.getAttachHtml());
                      if(gmt.getAttachHtml().equals("bla bla bla")) {
                          InputStream is = gmc.getAttachmentAsStream("1.0",gmt.getMatchingMessageID());
                          FileOutputStream out = new FileOutputStream("C:\\Renan\\&quot;+gmt.getAttachHtml());
                          PrintStream p = new PrintStream(out);
                          while(is.available() != 0) {
                              p.write(is.read());
                          }
                          break;
                      }
                  }
                  System.out.println("finished");
              }
              catch(Exception err) {
                  err.printStackTrace();
              }

      Well...

      Both in your code and mine, the InputStream's available() returns 0 so it doesn't enter the while clause.

      Can anyone here help me ?

      Thanx a lot,

      -Renan

       
    • iuri_oob
      iuri_oob
      2006-03-14

      But your trouble is not in the g4j APIs!.. Rather it is in the handling of the InputStream...
      Here is a method (that i'm using for may project) that takes a Vector of InputSteam and links them together into a file:

      public static File joinParts(Vector streams, String targetFilePath) throws Exception {
          File target = new File(targetFilePath);
          target.getParentFile().mkdirs();
          OutputStream out = new FileOutputStream(target);
          byte[] buf = new byte[1024];
          SequenceInputStream sis = new SequenceInputStream(streams.elements());
          for(int len; (len=sis.read(buf)) > 0; )
              out.write(buf, 0, len);
          sis.close();
          out.close();
          return target;
      }

      A suggestion: there is a site that shows code examples for ALL java packages, it's free: http://javaalmanac.com/egs/?

       
    • renanfsouza
      renanfsouza
      2006-03-16

      I'll take a look ;)

      Thanx a lot.

       
    • renanfsouza
      renanfsouza
      2006-03-17

      Here is my code. Please man... Tell me where is my error. I just can't get the attachment.

      try {
                  GMConnector gmc = new GMConnector("user","pass",1); 
                  gmc.connect();
                  GMResponse response = gmc.request(GMConstants.GM_REQ_STANDARD,"Inbox","0");
                  ArrayList msg = response.getGMThreads();
                  for(int i=0;i<msg.size();i++) {
                      GMThread gmt = (GMThread) msg.get(i);
                      System.out.println(gmt.getAttachHtml());
                      if(gmt.getAttachHtml().equals("bla bla bla")) {
                          InputStream is = gmc.getAttachmentAsStream("1.0",gmt.getMatchingMessageID());
                          File target = new File("C:\\Renan\\&quot;+gmt.getAttachHtml());
                          OutputStream out = new FileOutputStream(target);
                          byte[] buf = new byte[1024];
                          for(int len;(len=is.read(buf))>0;)
                              out.write(buf, 0, len);
                          break;
                      }
                  }
                  System.out.println("finished");
              }
              catch(Exception erro) {
                  erro.printStackTrace();
              }
          }

      Iuri... Could you please tell me and help to fix this code ?

      It generates a 61 bytes file, but the attachment has over 3MB.

       
    • iuri_oob
      iuri_oob
      2006-03-18

      having a first look i see two code lines are missing:

      is.close();
      out.close();

      try to add them after the for cycle (even if i don't think the problem is there..). The other code seems to be correct, are you sure of the name of your attachments?...try to add some other debugging println too, and make me know.. :)

       
    • renanfsouza
      renanfsouza
      2006-03-20

      try {
      GMConnector gmc = new GMConnector("user","pass",1); 
      gmc.connect();
      GMResponse response = gmc.request(GMConstants.GM_REQ_STANDARD,"Inbox","0");
      ArrayList msg = response.getGMThreads();
      for(int i=0;i<msg.size();i++) {
      GMThread gmt = (GMThread) msg.get(i);
      System.out.println(gmt.getAttachHtml());
      if(gmt.getAttachHtml().equals("bla bla bla")) {
      System.out.println("FOUNDED "+gmt.getAttachHtml()); //found bla bla bla
      InputStream is = gmc.getAttachmentAsStream("1.0",gmt.getMatchingMessageID());
      File target = new File("C:\\Renan\\&quot;+gmt.getAttachHtml());
      OutputStream out = new FileOutputStream(target);
      byte[] buf = new byte[1024];
      for(int len;(len=is.read(buf))>0;)
      out.write(buf, 0, len);
      break;
      }
      }
      System.out.println("finished");
      }
      catch(Exception erro) {
      erro.printStackTrace();
      }
      }

      Alright...

      I printed everything and it seamed to work fine.

      But I noticed the created file (the one with 61 bytes) contains the message subject ! So the InputStream "is" is receiving the message's subject from getAttachmentAsStream.

      Well... Hehehehe I'm a bit confused. Iuri... Could you help me again, please ?

      Thanx a lot.

      -Renan

       
    • renanfsouza
      renanfsouza
      2006-03-20

      Sorry...

      It's not receiving the message's subject. It's receiving the message's body.

       
    • iuri_oob
      iuri_oob
      2006-03-21

      yes, my code also does it... google is changing its apis... i'll try to fix it and than send you the code, ok?..

       
    • iuri_oob
      iuri_oob
      2006-03-21

      sorry again renan, the right attachment code is 0.1, NOT 1.0... :-P

       
    • renanfsouza
      renanfsouza
      2006-03-23

      Thanks a lot Iuri.

      I finally did it !