Search code examples
jakarta-mailcontent-typemultipart

While trying to read a multipart/signed mail, the original attachments are ignored and only smime.7ps file is displayed


I am trying to connect to a mailbox and read the messages and attachments. Here when there is any mail with a digital signature, only the smime.7ps file is read and others(xml,pdf etc.,) are ignored. I could observe that in such mails only the signature part of mail is read and body part is ignored. I am using Multipart here. Please let me know if there is any different way of handling which could help me to get the body part attachments read for mails with Digital Signature? Here is the part of my code which fetches the messages/attachments:

    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();
            }
        }

Solution

  • Thanks Shannon! Your input of nested multiparts actually helped me to solve the issue!

    MimeMultipart multiPart = (MimeMultipart) message.getContent(); //* Reading the Email Message & its contents*

    //***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);
                }
            }
    }