简体   繁体   中英

Reading Mails from Outlook Javamail

Am trying to read the emails from the outlook using javamail

herez the code snippet.

try {
      Properties props = new Properties();        
      props.setProperty("mail.store.protocol", "imap");
      props.setProperty("mail.imaps.starttls.enable", "true");    
      props.setProperty("mail.imaps.host", "outlook.office365.com");
      props.setProperty("mail.imaps.port", "143");    
      Session mailSession = Session.getInstance(props); 
      mailSession.setDebug(true);
      Store mailStore = mailSession.getStore("imaps");
      mailStore.connect("outlook.office365.com", "<username>", "<password>");                     
  } catch (Exception ex){
      ex.printStackTrace();
  }

Exception

javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:727)

Any pointers are appreciated.

Your code is very confused. Keep it simple:

  Properties props = new Properties();        
  props.setProperty("mail.imap.ssl.enable", "true");     
  Session mailSession = Session.getInstance(props); 
  mailSession.setDebug(true);
  Store mailStore = mailSession.getStore("imap");
  mailStore.connect("outlook.office365.com", "<username>", "<password>");

Use the "imap" protocol, but tell JavaMail to enable SSL. Don't worry about ports, JavaMail knows what to do. More detail in the JavaMail FAQ .

Port 143 (what you're using) is for plain-text IMAP (hence the error message plaintext connection? ). IMAPS uses port 993, so try that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM