I am trying to read mails from my outlook microsoft exchange server. Following is the code:
public void readEmailsFromOutlook(/*String host, String username, String password*/ ) throws MessagingException, IOException {
String host = "hostname";
String username = "domain\\username";
String password = "password"
// Create empty properties
Properties props = System.getProperties();
//
props.setProperty("mail.smtp.auth","true");
props.setProperty("mail.store.protocol","imaps");
props.setProperty("mail.imap.auth.plain.disable","true");
props.setProperty("mail.imap.host",host);
props.setProperty("mail.imap.port","993");
props.setProperty("mail.imap.user",username);
props.setProperty("mail.imap.pwd",password);
props.setProperty("mail.imap.debug","true");
props.setProperty("mail.imap.ssl.protocols","SSL");
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.imap.socketFactory.port", "993");
// Get session
Session session = Session.getInstance(props, new ExchangeAuthenticator(username, password));
session.setDebug(true);
// Get the store
Store store = session.getStore("imaps");
//Store store = session.getStore();
store.connect(host, username, password);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
// Get directory
Message message[] = folder.getMessages();
for (int i = 0, n = message.length; i < n; i++) {
System.out.println(i + ": " + message[i].getFrom()[0] + "\t"
+ message[i].getSubject());
System.out.println("Read message? [YES to read/QUIT to end]");
String line = reader.readLine();
if ("YES".equalsIgnoreCase(line)) {
System.out.println(message[i].getContent());
} else if ("QUIT".equalsIgnoreCase(line)) {
break;
}
}
// Close connection
folder.close(false);
store.close();
}
But it threw the following error:
DEBUG: setDebug: JavaMail version 1.5.1
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: trying to connect to host <hostname>,port 993, isSSL true
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 10.75.250.60, 993; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:693)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at com.capgemini.utilities.Utilities.readEmailsFromOutlook(Utilities.java:1261)
Whats wrong with my code?
Please help!!!!