Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EmailService {
- private String HOST = "imap.gmail.com";
- private String USERNAME = "mail@gmail.com";
- private String PASSWORD = "pass";
- private Properties properties;
- private Store store;
- private Folder inbox;
- public EmailService() throws MessagingException {
- this.properties = new Properties();
- this.properties.put("mail.imap.host", HOST);
- this.properties.put("mail.imap.port", "993");
- this.properties.put("mail.imap.starttls.enable", "true");
- }
- public void openEmailSession() throws MessagingException, InterruptedException {
- Session emailSession = Session.getInstance(this.properties);
- this.store = emailSession.getStore("imaps");
- this.store.connect(HOST, USERNAME, PASSWORD);
- this.inbox = this.store.getFolder("INBOX");
- this.inbox.open(Folder.READ_WRITE);
- }
- public void closeEmailSession() throws MessagingException, IOException {
- this.inbox.close(true);
- this.store.close();
- }
- public Message[] getUserMessages() throws MessagingException, IOException {
- Message[] messages = this.inbox.getMessages();
- return messages;
- }
- public void cleanInbox() throws IOException, MessagingException {
- Message[] messages = this.getUserMessages();
- for (Message message :messages) {
- message.setFlag(Flags.Flag.DELETED, true);
- }
- }
- }
- emailService.openEmailSession();
- emailService.cleanInbox();
- emailService.closeEmailSession();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement