Doubt JavaMail - Attachments coming null

0

I have an application that downloads the attachments that are sent in a certain email and works perfectly.

There was a need to do the same thing in another email. Both are gmail.

However, when I read the inbox of the second email, all attachments come as null and it does not make any sense to me.

Code:

public static void main(String[] args) {

    try {

        Email email = new Email();

        Store store = email.conectar(ConfigEmail.host, ConfigEmail.email, ConfigEmail.senha);

        Folder inbox = email.selecionaPasta(store);

        Message[] mensagens = email.mensagens(inbox);

        for (int i = 0; i < mensagens.length; i++) {

            System.out.println("Lendo: " + mensagens[i].getFrom()[0]);

            if(email.hasAttachments(mensagens[i])) {

                Multipart mp = (Multipart) mensagens[i].getContent();

                for (int j = 1; j <= mp.getCount() -1; j++) {

                    Part part = mp.getBodyPart(j);

                    // Exibe -1 para todos anexos
                    System.out.println(part.getSize());

                    // Exibe multipart/MIXED
                    System.out.println(part.getContentType());

                    // Exibe um endereco de memória
                    System.out.println(part.getContent());

                    //Exibe null
                    System.out.println(part.getFileName());
                }

            }
        }

        inbox.close(false);
        store.close();

    } catch (MessagingException | IOException e) {

        e.printStackTrace();
    }
}

I'm trying to download .XML files

I can not understand why the first email works perfectly and in the second, which is the same, come null.

    
asked by anonymous 23.10.2018 / 02:47

1 answer

1

SOLUTION:

For some reason, the email I'm trying to read, the attachments come in a different way. When I read a message from the inbox, I get the ContentType, if it's multipart / MIXED, it means that it has attachments, so I get the content of that message and make a for. For each iteration, I was expecting a different attachment, which I was not going to happen. In fact, another multipart / MIXED was coming. So I did a recursive method that solved this problem. Here is the code:

public void recursivo(Multipart mp) throws MessagingException, IOException {

    for (int i = 1; i < mp.getCount(); i++) {

        Part part = mp.getBodyPart(i);

        if (hasAttachments(part)) {

            Multipart multi = (Multipart) part.getContent();

            recursivo(multi);
        } else {

            if (part.getFileName().toUpperCase().contains(".XML")) {

                armazenaFile(part);
            }
        }
    }
}
    
23.10.2018 / 17:43