【问题标题】:How do I can fetch the email body without downloading the attachment file如何在不下载附件文件的情况下获取电子邮件正文
【发布时间】:2016-12-03 21:02:31
【问题描述】:

此问题与 Java Mail API 和 Gmail 帐户有关。

我想通过忽略附件文件来显示来自 gmail 电子邮件的消息部分。我的代码在没有附件的情况下可以正常工作,但是当涉及到带有附件的电子邮件时,它没有给出输出。

我只想显示电子邮件中的消息正文。

提前致谢。

【问题讨论】:

  • 添加您的代码,它可以在没有附件的情况下工作,如果您有任何日志以防附件,也请添加它。
  • 好的,问题部分来自第 76 行,不是吗?您是否尝试调试它?你知道那里会发生什么吗?我会尝试,但这些信息应该由您在您的问题中提供;)当我有更多空闲时间时,我会尝试调试它。

标签: java api email


【解决方案1】:

这只是第一次尝试调试代码,但您可以尝试关注original Oracle suggestion

private boolean textIsHtml = false;

    /**
     * Return the primary text content of the message.
     */
    private String getText(Part p) throws
                MessagingException, IOException {
        if (p.isMimeType("text/*")) {
            String s = (String)p.getContent();
            textIsHtml = p.isMimeType("text/html");
            return s;
        }

        if (p.isMimeType("multipart/alternative")) {
            // prefer html text over plain text
            Multipart mp = (Multipart)p.getContent();
            String text = null;
            for (int i = 0; i < mp.getCount(); i++) {
                Part bp = mp.getBodyPart(i);
                if (bp.isMimeType("text/plain")) {
                    if (text == null)
                        text = getText(bp);
                    continue;
                } else if (bp.isMimeType("text/html")) {
                    String s = getText(bp);
                    if (s != null)
                        return s;
                } else {
                    return getText(bp);
                }
            }
            return text;
        } else if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart)p.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                String s = getText(mp.getBodyPart(i));
                if (s != null)
                    return s;
            }
        }

        return null;
    }

我认为您尝试从中获取 MultiPart 和内容存在问题。 上面的代码仅在以下情况下有效:

您可以使用 Message 对象调用 getText 方法(它是 部分)。

【讨论】:

  • 我在现有代码中的何处添加此代码?我尝试添加它,但出现编译时错误?
猜你喜欢
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 2019-02-16
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多