Home > Java Core > Java Mail API- Reading OUTLOOK mails

Java Mail API- Reading OUTLOOK mails


Java Mail API- Reading OUTLOOK mails

Using JAVA Mail API for reading mails has been common these days. It’s pretty easy to use. Over the internet you can get boilerplate codes for reading mails from GMAIL or YAHOO, however I find a bit tricky when it comes to reading mails from OUTLOOK. GMAIL and YAHOO provides settings feature where you can turn ON for third-party apps access, whether POP3 or IMAP. But, for OUTLOOK the settings feature lets you handle POP3 only and these days developers prefer IMAP for reading mails. So, let me provide you some extra pieces of codes that lets you do IMAP access for OUTLOOK mails.

String protocol="imaps";
Properties props = new Properties();
props.setProperty("mail.store.protocol", protocol);
//extra codes required for reading OUTLOOK mails during IMAP-start
	props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	props.setProperty("mail.imaps.socketFactory.fallback", "false");
	props.setProperty("mail.imaps.port", "993");
	props.setProperty("mail.imaps.socketFactory.port", "993");
//extra codes required for reading OUTLOOK mails during IMAP-end
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore(protocol);
store.connect("imap-mail.outlook.com", emailAddress, password);
Folder inbox = store.getFolder("INBOX");

//search for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
Message messages[] = inbox.search(unseenFlagTerm);

About jkoder