How to read multipart mail using javamail

JavaMail provides a way to read multipart emails using the MimeMessage class. Here's an example of how to do it:

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class ReadMultipartEmail {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.store.protocol", "pop3");
        props.put("mail.pop3.host", "pop.gmail.com");
        props.put("mail.pop3.port", "995");
        props.put("mail.pop3.starttls.enable", "true");
        props.put("mail.pop3.auth", "true");
        props.put("mail.pop3.username", "[email protected]");
        props.put("mail.pop3.password", "your-password");

        Session session = Session.getInstance(props);
        Store store = session.getStore();
        store.connect();

        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        Message[] messages = folder.getMessages();
        for (Message message : messages) {
            if (message instanceof MimeMessage) {
                MimeMessage mimeMessage = (MimeMessage) message;
                Multipart multipart = (Multipart) mimeMessage.getContent();
                for (int i = 0; i < multipart.getCount(); i++) {
                    BodyPart bodyPart = multipart.getBodyPart(i);
                    if (bodyPart instanceof MimeBodyPart) {
                        MimeBodyPart mimeBodyPart = (MimeBodyPart) bodyPart;
                        String disposition = mimeBodyPart.getDisposition();
                        if (disposition!= null && disposition.equals("attachment")) {
                            // This is an attachment
                            String filename = mimeBodyPart.getFileName();
                            // You can save the attachment to a file
                            //...
                        } else {
                            // This is a text part
                            String content = new String(mimeBodyPart.getContent().toString());
                            System.out.println(content);
                        }
                    }
                }
            }
        }
        folder.close();
        store.close();
    }
}

In this example, we first connect to the POP3 server using the Session and Store classes. We then open the "INBOX" folder and retrieve the messages using the getMessages() method.

We then iterate over the messages and check if each message is a MimeMessage. If it is, we get the Multipart content using the getContent() method. We then iterate over the parts of the Multipart and check if each part is a MimeBodyPart. If it is, we get the disposition of the part using the getDisposition() method. If the disposition is "attachment", we know that this is an attachment and we can save it to a file. If the disposition is null or not "attachment", we know that this is a text part and we can print its content.

Note that this is just a basic example and you may need to add error handling and other features depending on your specific requirements.