Working with Message Attachments using IMAP

List Message Attachments using the IMAP Client

To get information about attachments such as name, size without fetching the attachment data, use the following API features:

The following code sample shows how to use the IMAP client to retrieve information about email messages and their attachments from a server and then display the attachment details for each message. It allows you to access and process attachments from email messages using the IMAP protocol.

ImapMessageInfoCollection messageInfoCollection = imapClient.listMessages();

for (ImapMessageInfo message : messageInfoCollection) {
    ImapAttachmentInfoCollection attachmentInfoCollection =
            imapClient.listAttachments(message.getSequenceNumber());

    for (ImapAttachmentInfo attachmentInfo : attachmentInfoCollection) {
        System.out.println(
                "Attachment: " + attachmentInfo.getName() + " (size: " + attachmentInfo.getSize() + ")");
    }
}