Doubt Javamail - Attachments coming null

Asked

Viewed 69 times

0

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

There arose the need to do the same thing in another email. The two are gmail.

However, when I read the inbox of the second email, all attachments come as null and this is not making 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 am trying to download . XML files

I can not understand why the first e-mail works perfectly and the second, which is the same thing, come null.

  • Could you separate the answer (in a new post) from the question?

  • 1

    dlosi, it’s cool that you share the solution found, but to be perfect really, it would be nice if you [Dit] your question, cutting out the solution, and pasting in the correct field (the answer field). Then you can mark your own answer as accepted, so the post is marked as solved.

  • I did. Thanks for the tips

1 answer

1


HICCUP:

For some reason, the e-mail I’m trying to read, the attachments come in a different way. When I read an Inbox message, I pick up Contenttype, if it’s Multipart/MIXED, it means it has attachments, so I take the contents of that message and I go. For each iteration, I was expecting a different attachment, the q I wasn’t happening. Actually, another Multipart/MIXED was coming. So I made a recursive method q solves this problem. Follow 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);
            }
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.