【问题标题】:Can't read Outlook mail with JavaMail, while Gmail works无法使用 JavaMail 读取 Outlook 邮件,而 Gmail 可以工作
【发布时间】:2012-12-07 21:56:10
【问题描述】:

基本上,我编写了一个从收件箱中读取电子邮件的应用程序。我一直使用从 Gmail 发送的电子邮件来测试该应用程序。但是现在当我尝试阅读从 Outlook 发送的电子邮件时,我没有收到任何内容。

我记录了两封电子邮件的内容类型: Gmail 返回:multipart/alternative; boundary=047d7b342bf2b6847f04d11df78a 展望回报:text/html; charset=iso-8859-1 注意:这些是相同的电子邮件,只是从不同的邮件客户端发送的。

来自 Gmail 的邮件将是 Multipart 的一个实例。而 Outlook 电子邮件将是 String 的一个实例。

我的代码:

检查消息是多部分实例还是字符串的方法。

public void getContent(Message msg) throws IOException, Exception {

    Object contt = msg.getContent();
    System.out.println("Contenttype: " + msg.getContentType());

    if (contt instanceof Multipart) {
        checkDisposition = true;
        handleMultipart((Multipart) contt);
    } else if (contt instanceof String) {   
       handlePart((Part) msg);
    }
    prepareEmail(mpMessage);
}

如果消息是多部分的,将调用此方法:

public void handleMultipart(Multipart multipart)
        throws MessagingException, IOException, Exception {
    mpMessage = getText(multipart.getBodyPart(0));

    for (int z = 1, n = multipart.getCount(); z < n; z++) {
        handlePart(multipart.getBodyPart(z));

    }
}

如果消息不是,这将被直接调用:

public void handlePart(Part part)
        throws MessagingException, IOException, Exception {



    Object con = messageCopy.getContent();

    String disposition = part.getDisposition();
    String contentType = part.getContentType();

    if (checkDisposition) {


        if (disposition == null) {

            System.out.println("Disposition is null");

        } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
            System.out.println("Attachment: " + part.getFileName()
                    + " : " + contentType);
            input = part.getInputStream();
            bytes = IOUtils.toByteArray(input);
        } else if (disposition.equalsIgnoreCase(Part.INLINE)) {
            System.out.println("Inline: "
                    + part.getFileName()
                    + " : " + contentType);
        } else {
            System.out.println("Other: " + disposition);
        }
    }else{
        mpMessage = part.getContent().toString(); //returns nothing



        System.out.println("mpMessage handlePart "+mpMessage); //returns nothing
        System.out.println("mpMessage handlePart "+part.getLineCount()); //returns 0
        System.out.println("mpMessage handlePart "+part.getContentType()); //returns text/html chartset=iso-8859-1
        System.out.println("mpMessage handlePart "+part.getSize()); // returns 22334
        part.writeTo(System.out); //See below

    }

}

从部件返回文本的方法:

private String getText(Part p) throws
        MessagingException, IOException {

    System.out.println("getText contentType "+p.getContentType());

//This part gets called if trying to read an Outlook mail, its not clear for me how to  retrieve the text from the part. Since `p.getContent()` returns nothing
    if (p.isMimeType("text/*")) {
        String s = (String) p.getContent();
        System.out.println();
        return String.valueOf(s);
    }

    if (p.isMimeType("multipart/alternative")) {
        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")) {
                String s = getText(bp);
                if (s != null) {
                    return s;
                }
            }
        }
        return text;
    }
    return null;
}

part.writeTo(System.out) 返回:

收到:来自 AMSPRD0710HT005.eurprd07.prod.outlook.com 服务器 (TLS) id 00000; 2012 年 12 月 20 日星期四 09:28:23 +0000 收到:来自 AMSPRD0710MB354.eurprd07.prod.outlook.com ([00.000.0000]) AMSPRD0710HT005.eurprd07.prod.outlook.com ([00.000.0000]) 与 mapi id 14.16.0245.002; 2012 年 12 月 20 日星期四 09:28:05 +0000 来自:测试 支持 主题:Verwerkingsverslag Kenmerk:0824496 主题:Verwerkingsverslag Kenmerk:0824496 线程索引:Ac3elFC2qYsSo+SOT2ii4HnbCCqgVw== 日期:2012 年 12 月 20 日星期四 10:28:05 +0100 消息 ID:...

等等。

消息本身的内容作为 HTML 代码返回,而不仅仅是普通文本。

如何从 Outlook 电子邮件中检索纯文本而不是 HTML 代码?或者如何在handlePart中检索part的内容?

感谢您的帮助,

谢谢!

【问题讨论】:

    标签: java email outlook gmail jakarta-mail


    【解决方案1】:

    您似乎假设 Outlook 将纯文本与 HTML 版本一起发送,但事实并非如此。您从 Outlook 记录的电子邮件的 MIME 类型是 text/html,这表明它只是一个 HTML 格式的文档。另一方面,Gmail 版本发送了multipart/alternative 的文档,这可能表明同一文档中有电子邮件的多个版本(纯文本和 HTML——我相信这是 Gmail 的默认行为)。因此,如果您获得的是 HTML 编码的版本,您将获得电子邮件的“文本”,就像它发送时一样。

    不要求使用纯文本版本或任何其他格式发送电子邮件。您可以确保邮件客户端以您的消费程序可以处理的格式发送电子邮件,或者更改消费程序以处理正在发送的格式。

    除了上述之外,您可能还想重新考虑这一行:

    mpMessage = getText(multipart.getBodyPart(0));
    

    这似乎假设多部分消息的第一部分将是纯文本文档和消息的文本。这可能是一个错误的假设。


    因此,假设您确实收到了包含 HTML 内容的邮件,getContent() 不应返回 null 或空字符串。它应该按照documentation on MimeBodyPart#getContent() 返回InputStream。阅读InputStream 应该使您能够生成带有HTML 标记的字符串。

    由于您似乎并不关心 HTML,而只关心内容,因此只需使用 Jsoup 等 Java HTML 解析库即可大大简化该过程。基本上,您可以通过将 getText() 更改为类似这样的方式将其集成到您当前的代码中:

    private String getText(Part p) throws MessagingException, IOException {
        System.out.println("getText contentType "+p.getContentType());
        if (p.isMimeType("text/plain")) {
            String s = (String) p.getContent();
            System.out.println(s);
            return s;
        } else if (p.isMimeType("text/html")) {
            // the last two parameters of this may need to be modified
            String s = Jsoup.parse(p.getInputStream(), null, null).text();
            System.out.println(s);
            return s;
        } else if (p.isMimeType("multipart/alternative")) {
            Multipart mp = (Multipart) p.getContent();
            String text = "";
            for (int i = 0; i < mp.getCount(); i++) {
                Part bp = mp.getBodyPart(i);
                if (bp.isMimeType("text/*")) {
                    String s = getText(bp);
                    if (s != null) {
                        text += s;
                    }
                }
            }
            return text;
        }
        return null;
    }
    

    请注意,这假设电子邮件足够小,可以完全在内存中读取和解析。

    【讨论】:

    • 感谢您的评论!非常有帮助。问题是我需要电子邮件的内容,我需要“扫描”某些单词。你建议用什么方法做到这一点?关于这条线,我用 Gmail 电子邮件对其进行了测试,它工作正常,但在 Outlook 电子邮件中遇到了同样的问题,叹息。我正在考虑设置一个 Gmail 帐户以将所有邮件转发到 Outlook 邮件客户端。
    • @Jef:见我上面的编辑。如果不清楚,请告诉我。
    • 非常感谢!对我帮助很大。
    猜你喜欢
    • 2017-11-09
    • 2014-09-13
    • 1970-01-01
    • 2014-11-17
    • 2011-12-02
    • 2018-04-23
    • 1970-01-01
    • 2015-05-21
    • 2012-01-28
    相关资源
    最近更新 更多