-2
I gave a search here but unsuccessfully. Someone knows how to find the latest email using the JavaMail
? What I’m using pulls all the emails. My code:
try {
campolog.setContentType("text/html");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(props);
Store store = emailSession.getStore("imaps");
store.connect("pop.gmail.com", "[email protected]", "senha");
// javax.mail.Folder[] folders = store.getDefaultFolder().list("*");
// for (javax.mail.Folder folder : folders) {
// if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
// System.out.println(folder.getFullName() + ": " + folder.getMessageCount());
// }
// }
Folder pastaEmail = store.getFolder("SMS");
pastaEmail.open(Folder.READ_ONLY);
Message[] mensagens = pastaEmail.getMessages();
System.out.println("Total de Emails: " + mensagens.length);
for (int i = 0, n = mensagens.length; i < n; i++) {
Message mensagem = mensagens[i];
//campolog.append("---------------------------------");
//campolog.append("Email Nº " + (i + 1));
//campolog.append("Assunto: " + mensagem.getSubject());
//campolog.setText("De: " + mensagem.getFrom()[0]);
campolog.setText("Mensagem: " + mensagem.getContent().toString());
}
pastaEmail.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Then in that case I would need to have the exact "position" of the email. Example: Email1, Email2....
– Abner Rodrigues