He resuelto este problema sin fin de esta manera.
Aviso:
- estoy usando el protocolo IMAP
- Yo sólo uso la conexión y esta clase para ver los mensajes de correo electrónico que he recibido
espero con este conjunto de propiedades de esta puede ayudar a muchos que han tenido problemas con esto a leer, escribir, lo que sea un correo electrónico, pero con perspectiva.
public class OutlookReader_imap {
Folder inbox;
// Constructor of the calss.
public OutlookReader_imap() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
/*
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
// set the message content here
Transport.send(msg, username, password);
*/
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imaps.socketFactory.fallback", "false");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.imaps.socketFactory.port", "993");
props.put("mail.imaps.host", "imap-mail.outlook.com");
try {
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(System.getProperties(), null);
Store store = session.getStore("imaps");
store.connect("imap-mail.outlook.com", 993, "<email>", "<password>");
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + inbox.getUnreadMessageCount());
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(messages, fp);
try {
printAllMessages(messages);
inbox.close(true);
store.close();
} catch (Exception ex) {
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
} catch (MessagingException e) {
System.out.println("Exception while connecting to server: " + e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception {
for (int i = 0; i < msgs.length; i++) {
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception {
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("De : " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
//Dar Formato a la fecha
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Asunto : " + subject);
if (receivedDate != null) {
System.out.println("Recibido: " + df.format(receivedDate));
}
System.out.println("Enviado : " + df.format(sentDate));
}
public static void main(String args[]) {
new OutlookReader_imap();
}
}
¡Esto podría ser un problema de tiempo real para los chicos de producción! – Jayy