gpt4 book ai didi

javax.mail : Get nested attachments in EML attachment

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:47:03 28 4
gpt4 key购买 nike

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

要处理的电子邮件必须有一个或多个 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 并递归地获取附件,但我错过了全局。

更新:我已经修改了processAttachment 方法(但它仍然不起作用):

private void processAttachment(final Multipart multipart) {

try {
for (int i = 0; i < multipart.getCount(); i++) {
final BodyPart bodyPart = multipart.getBodyPart(i);

if (bodyPart.getContent() instanceof Multipart) {
// part-within-a-part, do some recursion...
extractAttachment((Multipart) bodyPart.getContent());
}

System.out.println("Filename: " + bodyPart.getFileName());
System.out.println("ct: " + bodyPart.getContentType());

final boolean isXml = bodyPart.getFileName() != null && isXmlType(bodyPart.getContentType());
if (isXml) {
final InputStream inputStream = getReusableInputStream(bodyPart);
processXMLAttachment(inputStream);
}

}
} [cutted]

}

输出是:

Filename: null
ct: TEXT/PLAIN; charset=iso-8859-1
Filename: null
ct: TEXT/HTML; charset=iso-8859-1
Filename: daticert.xml
ct: APPLICATION/XML; name=daticert.xml
Filename: postacert.eml
ct: MESSAGE/RFC822; name=postacert.eml
Filename: smime.p7s
ct: APPLICATION/X-PKCS7-SIGNATURE; name=smime.p7s

从输出中,我可以看到系统只扫描了第一级附件 daticert.xmlpostacert.eml 但没有找到嵌套附件。

更具体地说,我必须阅读以下内容:

Filename: postacert.eml
ct: MESSAGE/RFC822; name=postacert.eml

有什么帮助吗?

谢谢

最佳答案

好吧,我通过检查任何 MimePart 的类来解决,我发现嵌套消息是 IMAPNestedMessage 的类型,所以在这种对象上我递归调用主要方法processMessage:

private void processAttachment(final Multipart multipart) {

try {
for (int i = 0; i < multipart.getCount(); i++) {
final BodyPart bodyPart = multipart.getBodyPart(i);

// BEGIN - Added this part
System.out.println("CLASS bodyPart: " + bodyPart.getContent().getClass());

if (bodyPart.getContent() instanceof IMAPNestedMessage) {
processMessage((IMAPNestedMessage) bodyPart.getContent());
} else {
// END - Added this part
if (bodyPart.getContent() instanceof Multipart) {
processAttachment((Multipart) bodyPart.getContent());
} else {
final boolean isXml = bodyPart.getFileName() != null && isXmlType(bodyPart.getContentType());
if (isXml) {
final InputStream inputStream = getReusableInputStream(bodyPart);
processXMLAttachment(inputStream);
}
}
}

}
} catch (final Exception e) {
sendMailService.sendMailForImportINPSFailed("metodo processAttachment()", e);
e.printStackTrace();
}

}

现在它工作正常。

关于javax.mail : Get nested attachments in EML attachment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51405004/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com