title: JavaMail BaseEncode64 Error
body: <p>I'm currently developing an application which download attachment from gmail account.
  Right now, I got error whenever downloading zipped attachment. But, not all, some I can retrieve it without error. Here's the Exception message:</p>
  
  <pre><code>Exception in thread "main" com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 1 before EOF, the 10 most recent characters were: "Q3w5ilxj2P"
  </code></pre>
  
  <p>FYI: I was able to download the attachment via gmail web interface.</p>
  
  <p>Here's the snippet:</p>
  
  <pre><code>        Multipart multipart = (Multipart) message.getContent();
  
          for (int i = 0; i &lt; multipart.getCount(); i++) {
  
              BodyPart bodyPart = multipart.getBodyPart(i);
  
              if (bodyPart.getFileName().toLowerCase().endsWith("zip") ||
                      bodyPart.getFileName().toLowerCase().endsWith("rar")) {
                  InputStream is = bodyPart.getInputStream();
                  File f = new File("/tmp/" + bodyPart.getFileName());
                  FileOutputStream fos = new FileOutputStream(f);
                  byte[] buf = new byte[bodyPart.getSize()];
                  int bytesRead;
                  while ((bytesRead = is.read(buf)) != -1) {
                      fos.write(buf, 0, bytesRead);
                  }
                  fos.close();
              }
          }
      }
  </code></pre>
  
  <p>Anyone have idea, how to work around this problem?</p>