code_junkie

Getting mail from GMail into Java application using IMAP

Nov 10th, 2011
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. Properties props = System.getProperties();
  2. props.setProperty("mail.imap.host", "imap.gmail.com");
  3. props.setProperty("mail.imap.port", "993");
  4. props.setProperty("mail.imap.connectiontimeout", "5000");
  5. props.setProperty("mail.imap.timeout", "5000");
  6.  
  7. try {
  8. Session session = Session.getDefaultInstance(props, new MyAuthenticator());
  9. URLName urlName = new URLName("imap://[email protected]:[email protected]");
  10. Store store = session.getStore(urlName);
  11. if (!store.isConnected()) {
  12. store.connect();
  13. }
  14. } catch (NoSuchProviderException e) {
  15. e.printStackTrace();
  16. System.exit(1);
  17. } catch (MessagingException e) {
  18. e.printStackTrace();
  19. System.exit(2);
  20. }
  21.  
  22. Properties props = System.getProperties();
  23. props.setProperty("mail.store.protocol", "imaps");
  24. try {
  25. Session session = Session.getDefaultInstance(props, null);
  26. Store store = session.getStore("imaps");
  27. store.connect("imap.gmail.com", "<username>@gmail.com", "<password>");
  28. ...
  29. } catch (NoSuchProviderException e) {
  30. e.printStackTrace();
  31. System.exit(1);
  32. } catch (MessagingException e) {
  33. e.printStackTrace();
  34. System.exit(2);
  35. }
  36.  
  37. imaps
Add Comment
Please, Sign In to add comment