JavaMail BaseEncode64错误 [英] JavaMail BaseEncode64 Error

查看:1201
本文介绍了JavaMail BaseEncode64错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个从Gmail帐户下载附件的应用程序。
现在,每次下载压缩附件时都会出错。但是,并非全部,有些我可以无误地检索它。这里是Exception消息:

pre $ 线程main中的异常com.sun.mail.util.DecodingException:BASE64Decoder:编码错误stream:需要4个有效的base64字符,但在EOF之前只有1个字符,最近的10个字符是:Q3w5ilxj2P





以下是代码片段:

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

for(int i = 0; i< multipart.getCount(); i ++){

BodyPart bodyPart = multipart.getBodyPart(i); $())
$ b $ 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; ((bytesRead = is.read(buf))!= -1){
fos.write(buf,0,bytesRead);
}
fos.close();
}
}
}

任何人都有想法,如何解决这个问题?

解决方案

从已知限制,错误,JavaMail问题列表:


某些IMAP服务器没有正确执行
IMAP部分获取功能
。当从
下载IMAP服务器的大量邮件时,此问题通常为
,表现为损坏的电子邮件附件
。要解决此
服务器错误,请将
mail.imap.partialfetch属性设置为
false。您必须在您提供给会话的
属性对象中设置此
属性。


所以你应该在imap会话中关闭部分提取。例如:

 属性props = System.getProperties(); 
props.setProperty(mail.store.protocol,imaps);
props.setProperty(mail.imaps.partialfetch,false);
Session session = Session.getDefaultInstance(props,null);
Store store = session.getStore(imaps);
store.connect(imap.gmail.com,< username>,< password>);


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:

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"

FYI: I was able to download the attachment via gmail web interface.

Here's the snippet:

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

        for (int i = 0; i < 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();
            }
        }
    }

Anyone have idea, how to work around this problem?

解决方案

From a list of the known limitations, bugs, issues of JavaMail:

Certain IMAP servers do not implement the IMAP Partial FETCH functionality properly. This problem typically manifests as corrupt email attachments when downloading large messages from the IMAP server. To workaround this server bug, set the "mail.imap.partialfetch" property to false. You'll have to set this property in the Properties object that you provide to your Session.

So you should just turn off partial fetch in imap session. For example:

Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.partialfetch", "false");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "<username>","<password>");

这篇关于JavaMail BaseEncode64错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆