简体   繁体   中英

Downloading a word Document from Gmail using Javamail

I'm using Javamail API, and I'm trying to download an attachment file, for example a word file. The problem is I get a base64 decoding exception while trying to read the bytes and save it to a file. I'm using the following code for that purpose.

Stack Exception:

IOException:com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 2 before EOF, the 10 most recent characters were: "AG4AAAAAAA"

JavaMail Code:

 private void getAttachments(Message temp) throws IOException, MessagingException {
        List<File> attachments = new ArrayList<File>();

        Multipart multipart = (Multipart) temp.getContent();

        System.out.println(multipart.getCount());

        for (int i = 0; i < multipart.getCount(); i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                continue; // dealing with attachments only
            }
            InputStream is = bodyPart.getInputStream();


            File f = new File("C:\\Attachments\\" + bodyPart.getFileName());

          //  saveFile(bodyPart.getFileName(),is);

            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            while (br.ready()) {
                System.out.println(br.readLine());
            }



           saveFile(bodyPart.getFileName(),is);

            attachments.add(f);
        }



public static void saveFile(String filename,InputStream input) 
    {

        System.out.println(filename);
        try {
            if (filename == null) {
                //filename = File.createTempFile("VSX", ".out").getName();
                return;
            }
            // Do no overwrite existing file
            filename = "C:\\Attachments\\" + filename;
            File file = new File(filename);
            for (int i = 0; file.exists(); i++) {
                file = new File(filename + i);
            }
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            BufferedInputStream bis = new BufferedInputStream(input);


            int aByte;
            while ((aByte = bis.read()) >=0) {
                bos.write(aByte);
            }

            bos.flush();
            bos.close();
            bis.close();
        } // end of try()
        catch (IOException exp) {
            System.out.println("IOException:" + exp);
        }
    } //end of saveFile()

You need to disable partial fetch to get rid of this issue. Here is how you can do that

props.setProperty("mail.imap.partialfetch", "false");

or

props.setProperty("mail.imaps.partialfetch", "false");

if you are working using imaps.

Here is the full reference: JavaMail BaseEncode64 Error

Use the saveFile() method from MimeBodyPart. And if you really want to role your own stop(!) reading lines from the inputstream before handing it over to your saveFile method. Your are consuming part of the content, which may contribute to the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM