javax.mail:在 EML 附件中获取嵌套附件

我有一个现有的代码,可以正确下载和处理一些电子邮件。


要处理的电子邮件必须有一个或多个 xml 作为附件,现在我正在将此过程从当前的标准邮件帐户迁移到一个经过认证的系统,该系统将该邮件包装成新的电子邮件。


因此,我必须解析带有 XML(经过认证的)和 EML(我应该处理的消息)的电子邮件,而不是带有一个 xml 附件的平面电子邮件。


简而言之,我的代码如下:


private void processMessage(final Message message) {

    try {

        final String contentType = message.getContentType();

        if (contentType.contains("multipart")) {

            final Multipart multiPart = (Multipart) message.getContent();


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

                final MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);


                /**************************************************************

                 * HERE I CAN'T GET THE EML (and its attachments) FROM 'part' *

                 **************************************************************/


                if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {

                    processAttachment(part);

                }

            }

        }

    } [...cutted...]

}


private void processAttachment(final MimeBodyPart part) throws IOException, MessagingException {

    final InputStream input = getReusableInputStream(part);


    if (part.getFileName() != null && isXmlType(part.getContentType())) {

        processXml(input);

    }

}

我应该修改它,以便解析 EML 并递归获取附件,但我错过了大局。


慕尼黑的夜晚无繁华
浏览 176回答 1
1回答

胡子哥哥

好吧,我通过检查 any 的类解决了MimePart,我发现嵌套消息的类型是IMAPNestedMessage,所以在这种对象上我递归调用 main 方法processMessage:private void processAttachment(final Multipart multipart) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < multipart.getCount(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final BodyPart bodyPart = multipart.getBodyPart(i);// BEGIN - Added this part&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("CLASS bodyPart: " + bodyPart.getContent().getClass());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bodyPart.getContent() instanceof IMAPNestedMessage) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processMessage((IMAPNestedMessage) bodyPart.getContent());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {// END - Added this part&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bodyPart.getContent() instanceof Multipart) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processAttachment((Multipart) bodyPart.getContent());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final boolean isXml = bodyPart.getFileName() != null && isXmlType(bodyPart.getContentType());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isXml) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final InputStream inputStream = getReusableInputStream(bodyPart);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processXMLAttachment(inputStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (final Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; sendMailService.sendMailForImportINPSFailed("metodo processAttachment()", e);&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}现在它工作正常。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java