【问题标题】:JavaMail BaseEncode64 ErrorJavaMail BaseEncode64 错误
【发布时间】:2010-12-17 20:16:59
【问题描述】:

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

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"

仅供参考:我可以通过 gmail Web 界面下载附件。

这是sn-p:

        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();
            }
        }
    }

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

【问题讨论】:

  • 不,还没有。似乎。没有人对 java 邮件感兴趣:(

标签: gmail jakarta-mail multipart


【解决方案1】:

来自JavaMail的known limitations, bugs, issues列表:

某些 IMAP 服务器未实现 IMAP 部分 FETCH 功能 适当地。这个问题一般 表现为损坏的电子邮件附件 当从 IMAP 服务器。要解决此问题 服务器错误,设置 “mail.imap.partialfetch”属性 错误的。你必须设置这个 Properties 对象中的属性 您提供给您的会话。

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

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

来源:https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/package-summary.html

【讨论】:

  • 如果这不能解决问题怎么办?我已经设置了properties.setProperty("mail.imaps.partialfetch", "false");(我也尝试过设置“mail.imap.partialfetch”,这也没有帮助),但我仍然遇到“BASE64Decoder:编码流中的错误:需要4个有效的base64”类型的异常字符,但在 EOF 之前只有 1 个字符”
【解决方案2】:

如果您使用的是 java 邮件 API,请在连接 imap 服务器时添加这些行......

Properties prop = new Properties();
prop.put("mail.imaps.partialfetch", false);
Session session = Session.getDefaultInstance(prop, null);

........ ....你的代码.. ......

它应该可以工作。

【讨论】:

  • 根据documentation,属性值必须始终设置为字符串。看来您使用的布尔值可能不会有任何影响。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 2012-03-14
  • 2015-06-01
  • 2012-01-10
  • 2012-01-15
  • 1970-01-01
相关资源
最近更新 更多