Advertisement
Guest User

Untitled

a guest
Sep 11th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. public class EmailService {
  2.  
  3. private String HOST = "imap.gmail.com";
  4. private String USERNAME = "mail@gmail.com";
  5. private String PASSWORD = "pass";
  6. private Properties properties;
  7. private Store store;
  8. private Folder inbox;
  9.  
  10. public EmailService() throws MessagingException {
  11. this.properties = new Properties();
  12. this.properties.put("mail.imap.host", HOST);
  13. this.properties.put("mail.imap.port", "993");
  14. this.properties.put("mail.imap.starttls.enable", "true");
  15. }
  16.  
  17. public void openEmailSession() throws MessagingException, InterruptedException {
  18. Session emailSession = Session.getInstance(this.properties);
  19. this.store = emailSession.getStore("imaps");
  20. this.store.connect(HOST, USERNAME, PASSWORD);
  21.  
  22. this.inbox = this.store.getFolder("INBOX");
  23. this.inbox.open(Folder.READ_WRITE);
  24. }
  25.  
  26. public void closeEmailSession() throws MessagingException, IOException {
  27. this.inbox.close(true);
  28. this.store.close();
  29. }
  30.  
  31. public Message[] getUserMessages() throws MessagingException, IOException {
  32. Message[] messages = this.inbox.getMessages();
  33. return messages;
  34. }
  35.  
  36. public void cleanInbox() throws IOException, MessagingException {
  37. Message[] messages = this.getUserMessages();
  38. for (Message message :messages) {
  39. message.setFlag(Flags.Flag.DELETED, true);
  40. }
  41. }
  42. }
  43.  
  44. emailService.openEmailSession();
  45. emailService.cleanInbox();
  46. emailService.closeEmailSession();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement