我正在尝试连接到邮箱并阅读邮件和附件。这里有任何带有数字签名的邮件时,只读取smime.7ps文件,忽略其他文件(xml,pdf等)。我可以观察到,在这些邮件中,只读取邮件的签名部分,忽略正文部分。我在这里使用Multipart。请告诉我是否有任何不同的处理方式可以帮助我使用数字签名获取邮件的身体部分附件?以下是我的代码中获取消息/附件的部分:
if (contentType.contains("multipart")){
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile(SaveDirectory + File.separator + fileName);
} else {
// this part may be the message content
messageContent = part.getContent().toString();
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
//}
} else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}
}
谢谢香农!您对嵌套多部件的输入实际上帮助我解决了这个问题!
MimeMultipart multiPart =(MimeMultipart)message.getContent(); // *阅读电子邮件及其内容*
//***Your code for Different actions with Email Message
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
//***Reading Body Part contents from the Email Message
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
//***Your Code for Different actions with Body part contents
//***Now the below step would help you to check if the above retrieved content(part) is having any further multiparts nested in it.
//***Once the check is true, then you can instantiate that content again as a multipart and retrieve the related details.
if(part.getContent() instanceof Multipart){
Multipart multipart = (Multipart) part.getContent();
for (int j = 0; j < multipart.getCount(); j++) {
MimeBodyPart bodyPart = (MimeBodyPart)multipart.getBodyPart(j);
}
}
}