如何使用JavaMail处理多部分/备用邮件?

Jef*_*Jef 11 java email attachment multipart jakarta-mail

我编写了一个应用程序,它从收件箱中获取所有电子邮件,过滤包含特定字符串的电子邮件,然后将这些电子邮件放入ArrayList中.

将电子邮件放入列表后,我正在处理所述电子邮件的主题和内容.这适用于没有附件的电子邮件.但是当我开始使用带有附件的电子邮件时,它们都不能按预期工作了.

这是我的代码:

public void getInhoud(Message msg) throws IOException {
    try {
        cont = msg.getContent();
    } catch (MessagingException ex) {
        Logger.getLogger(ReadMailNew.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (cont instanceof String) {
        String body = (String) cont;


    } else if (cont instanceof Multipart) {
        try {
            Multipart mp = (Multipart) msg.getContent();
            int mp_count = mp.getCount();
            for (int b = 0; b < 1; b++) {
                    dumpPart(mp.getBodyPart(b));
            }
        } catch (Exception ex) {
            System.out.println("Exception arise at get Content");
            ex.printStackTrace();
        }
    }
}

public void dumpPart(Part p) throws Exception {
    email = null;
    String contentType = p.getContentType();
    System.out.println("dumpPart" + contentType);
    InputStream is = p.getInputStream();
    if (!(is instanceof BufferedInputStream)) {
        is = new BufferedInputStream(is);
    }
    int c;
    final StringWriter sw = new StringWriter();
    while ((c = is.read()) != -1) {
        sw.write(c);
    }

    if (!sw.toString().contains("<div>")) {
        mpMessage = sw.toString();
        getReferentie(mpMessage);
    }
}
Run Code Online (Sandbox Code Playgroud)

电子邮件中的内容存储在String中.

当我尝试阅读没有附件的邮件时,此代码可以正常工作.但是,如果我使用带附件的电子邮件,String也包含HTML代码甚至附件编码.最终我想存储附件和电子邮件的内容,但我的首要任务是只获取没有任何HTML或附件编码的文本.

现在我尝试了一种不同的方法来处理不同的部分:

public void getInhoud(Message msg) throws IOException {
    try {
        Object contt = msg.getContent();

        if (contt instanceof Multipart) {
            System.out.println("Met attachment");
            handleMultipart((Multipart) contt);
        } else {
            handlePart(msg);
            System.out.println("Zonder attachment");

        }
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
}

public static void handleMultipart(Multipart multipart)
        throws MessagingException, IOException {
    for (int i = 0, n = multipart.getCount(); i < n; i++) {
        handlePart(multipart.getBodyPart(i));
        System.out.println("Count "+n);
    }
}

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

    String disposition = part.getDisposition();
    String contentType = part.getContentType();
    if (disposition == null) { // When just body
        System.out.println("Null: " + contentType);
        // Check if plain
        if ((contentType.length() >= 10)
                && (contentType.toLowerCase().substring(
                0, 10).equals("text/plain"))) {
            part.writeTo(System.out);
        } else if ((contentType.length() >= 9)
                && (contentType.toLowerCase().substring(
                0, 9).equals("text/html"))) {
            part.writeTo(System.out);
        } else if ((contentType.length() >= 9)
                && (contentType.toLowerCase().substring(
                0, 9).equals("text/html"))) {
            System.out.println("Ook html gevonden");
            part.writeTo(System.out);
        }else{
            System.out.println("Other body: " + contentType);
            part.writeTo(System.out);
        }
    } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
        System.out.println("Attachment: " + part.getFileName()
                + " : " + contentType);
    } else if (disposition.equalsIgnoreCase(Part.INLINE)) {
        System.out.println("Inline: "
                + part.getFileName()
                + " : " + contentType);
    } else {
        System.out.println("Other: " + disposition);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是从...返回的内容 System.out.printlns

Null: multipart/alternative; boundary=047d7b6220720b499504ce3786d7
Other body: multipart/alternative; boundary=047d7b6220720b499504ce3786d7
Content-Type: multipart/alternative; boundary="047d7b6220720b499504ce3786d7"

--047d7b6220720b499504ce3786d7
Content-Type: text/plain; charset="ISO-8859-1"

'Text of the message here in normal text'

--047d7b6220720b499504ce3786d7
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable

'HTML code of the message'
Run Code Online (Sandbox Code Playgroud)

此方法返回电子邮件的正常文本,但也返回邮件的HTML编码.我真的不明白为什么会发生这种情况,我用谷歌搜索了它,但似乎没有其他人遇到这个问题.

任何帮助表示赞赏,

谢谢!

kev*_*rpe 23

我发现使用JavaMail库阅读电子邮件要比预期困难得多.我不怪JavaMail API,而是因为我对RFC-822(对Internet电子邮件的官方定义)的理解不好.

作为思想实验:考虑一下电子邮件在现实世界中的复杂程度.可以在消息中"无限地"嵌入消息.每条消息本身可能有多个附件(二进制或人类可读的文本).现在想象一下解析后这个结构在JavaMail API中的复杂程度.

使用JavaMail遍历电子邮件时可能有所帮助的一些提示:

Message,MultipartBodyPart所有实施Part.如果可能的话,把一切作为Part.这将允许更容易地构建通用遍历方法.

这些Part方法将有助于遍历:

  • String getContentType():以MIME类型开头.您可能会将此视为MIME类型(有一些黑客/剪切/匹配),但不要.最好只在调试器中使用此方法进行检查.
    • 奇怪的是,无法直接提取MIME类型.而是boolean isMimeType(String)用来匹配.仔细阅读文档以了解强大的通配符,例如"multipart/*".
  • Object getContent():可能是instanceof:
    • Multipart- 容器更多Parts
      • 转换为Multipart,然后使用int getCount()和迭代为从零开始的索引BodyPart getBodyPart(int)
        • 注意:BodyPart工具Part
      • 根据我的经验,Microsoft Exchange服务器定期提供正文文本的两个副本:纯文本和HTML.
        • 要匹配纯文本,请尝试: Part.isMimeType("text/plain")
        • 要匹配HTML,请尝试: Part.isMimeType("text/html")
    • Message(实现Part) - 嵌入或附加的电子邮件
    • String (只是正文 - 纯文本或HTML)
      • 请参阅上面有关Microsoft Exchange服务器的说明.
    • InputStream (可能是BASE64编码的附件)
  • String getDisposition():值可能为null
    • if Part.ATTACHMENT.equalsIgnoreCase(getDisposition()),然后调用getInputStream()获取附件的原始字节.

最后,我发现官方的Javadocs排除了com.sun.mail包中的所有内容(可能还有更多内容).如果需要这些,请直接读取代码,或者通过下载源代码mvn javadoc:javadocmail项目的项目模块中运行来生成未过滤的Javadoc .