java - unable to read outlook emails with javamail -


i'm writing program read emails outlook.

i wrote below program send email.

package com.getemails;  import java.util.properties; import javax.mail.*; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage;  public class mailexample {     public static void main(string args[]) {          string = "xyz@abc.com";         string = "ubv@abc.com";         string host = "myhost";          try {             // system properties             properties props = system.getproperties();              authenticator authenticator = new authenticator();             props.put("mail.smtp.host", host);             props.put("mail.smtp.auth", "true");             // props.put("mail.smtp.auth.mechanisms","ntlm");             props.put("mail.smtp.submitter", authenticator                     .getpasswordauthentication().getusername());             // session             session session = session.getinstance(props, authenticator);              session.setdebug(true);              // define message             mimemessage message = new mimemessage(session);              // set address             message.setfrom(new internetaddress(from));              // set address             message.addrecipient(message.recipienttype.to, new internetaddress(                     to));              // set subject             message.setsubject("javamail test");              // set content             message.settext("test email through java mail");             // send message             transport.send(message);             system.out.println("ok man");         }          catch (messagingexception e) {             e.tostring();         } catch (exception mex) {             system.out.print(mex);         }     }      static class authenticator extends javax.mail.authenticator {         private passwordauthentication authentication;          public authenticator() {             string username = "";             string password = "";             authentication = new passwordauthentication(username, password);         }          protected passwordauthentication getpasswordauthentication() {             return authentication;         }     } } 

here i've given host myhost, i'm sorry that, since organizations host address.

and i'm trying read contents of inbox using below program.

package com.getemails;  import java.util.properties;  import javax.mail.folder; import javax.mail.message; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.store;  public class reademails {      public static void main(string[] args) throws exception {         final string username = "xyz@abc.com";         final string password = "password";         final string host = "myhost";         // properties         properties props = system.getproperties();          session session = session.getinstance(props,                 new javax.mail.authenticator() {                     protected passwordauthentication getpasswordauthentication() {                         return new passwordauthentication(username, password);                     }                 });         session.setdebug(true);          // store object         store store = session.getstore("imaps");         store.connect(host, username, password);          // folder         folder folder = store.getfolder("drafts");         folder.open(folder.read_only);         message messages[] = folder.getmessages();         (message message : messages) {             system.out.println(message.getsubject());         }     }  } 

here when run above program i'm getting below output.

debug: setdebug: javamail version 1.4.5 debug: getprovider() returning javax.mail.provider[store,imaps,com.sun.mail.imap.imapsslstore,sun microsystems, inc] debug: mail.imap.fetchsize: 16384 debug: mail.imap.statuscachetimeout: 1000 debug: mail.imap.appendbuffersize: -1 debug: mail.imap.minidletime: 10 debug: trying connect host "myhost", port 993, isssl true exception in thread "main" javax.mail.messagingexception: connection timed out: connect;   nested exception is:     java.net.connectexception: connection timed out: connect     @ com.sun.mail.imap.imapstore.protocolconnect(imapstore.java:670)     @ javax.mail.service.connect(service.java:295)     @ javax.mail.service.connect(service.java:176)     @ com.getemails.reademails.main(reademails.java:30) caused by: java.net.connectexception: connection timed out: connect     @ java.net.dualstackplainsocketimpl.connect0(native method)     @ java.net.dualstackplainsocketimpl.socketconnect(unknown source)     @ java.net.abstractplainsocketimpl.doconnect(unknown source)     @ java.net.abstractplainsocketimpl.connecttoaddress(unknown source)     @ java.net.abstractplainsocketimpl.connect(unknown source)     @ java.net.plainsocketimpl.connect(unknown source)     @ java.net.sockssocketimpl.connect(unknown source)     @ java.net.socket.connect(unknown source)     @ java.net.socket.connect(unknown source)     @ com.sun.mail.util.socketfetcher.createsocket(socketfetcher.java:319)     @ com.sun.mail.util.socketfetcher.getsocket(socketfetcher.java:233)     @ com.sun.mail.iap.protocol.<init>(protocol.java:113)     @ com.sun.mail.imap.protocol.imapprotocol.<init>(imapprotocol.java:111)     @ com.sun.mail.imap.imapstore.protocolconnect(imapstore.java:637)     ... 3 more 

here if change imaps imap, i'm getting same error.

and want tell used same host both reading , writing emails.

please let me know how can fix , emails. i'm using javamail(i've use this).

by "outlook" assume you're connecting microsoft exchange mail server in organization.

exchange uses microsoft proprietary protocol outlook mail reader. unless imap support has been enabled on exchange server, won't able connect using javamail. check server administrator see if imap has been enabled. (enabling imap reading separate enabling smtp sending.)

also, you're using old version of javamail. should consider upgrading current version.


Comments