Friday, June 17, 2016

Java Mail API

We are accessing Gmail mail server in our application.We can get all or selected unread mails from inbox using IMAP as protocol. Then we can read or delete the emails.
  1. First have to create the Store using imap protocol
    private static Store getConnection() throws MessagingException {
     Properties properties;
     Session session;
     Store store;
     properties = new Properties();
     properties.setProperty("mail.host", "imap.gmail.com");
     properties.setProperty("mail.port", "995");
     properties.setProperty("mail.transport.protocol", "imaps");
     session = Session.getInstance(properties,new javax.mail.Authenticator() {
     protected PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(receiver + "@" + domain,String.valueOf(receiverPasword));
     }
     });
     try {
     store = session.getStore("imaps");
     store.connect();
     return store;
     } catch (MessagingException e) {
     log.error("Error while connecting to email store ", e);
     throw new MessagingException("Error while connecting to email store ", e);
     }
    }
    
  2. Using the store you can read all the unread emails as below.
       Store store = null;
       Folder inbox = null;
       boolean emailReceived = false;
        try {
         store = getConnection();
         inbox = store.getFolder("INBOX");
         inbox.open(Folder.READ_WRITE);
     Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
       for (Message message : messages) {
         log.info("email subject : " + message.getSubject());
         }
        } catch (MessagingException ex) {
           log.error("Error when getting mail count", ex);
            throw new MessagingException("Error when getting mail count " + ex.getMessage(), ex);
        } finally {
          if (inbox != null) {
            inbox.close(true);
           }
         if (store != null) {
             store.close();
          }
        }
    
    
  3. Or you can select emails from the store by subject contain as below.
     Store store = null;
     Folder inbox = null;
     boolean emailReceived = false;
       try {
         store = getConnection();
         Folder mailFolder = store.getFolder(folder);
         mailFolder.open(Folder.READ_WRITE);
         SearchTerm st = new AndTerm(new SubjectTerm(subject), new BodyTerm(subject));
         Message[] messages = mailFolder.search(st);
         for (Message message : messages) {
           if (message.getSubject().contains(subject)) {
             log.info("Found the email subject : " + subject);
            }
          }
       } catch (MessagingException ex) {
           log.error("Error when getting mail count", ex);
           throw new MessagingException("Error when getting mail count " + ex.getMessage(), ex);
       } finally {
          if (inbox != null) {
            inbox.close(true);
           }
          if (store != null) {
             store.close();
           }
       }
    
    
  4. Delete emails as below
 Store store = null;
 Folder inbox = null;
   try {
     store = getConnection();
     inbox = store.getFolder("INBOX");
     inbox.open(Folder.READ_WRITE);
     Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
      for (Message message : messages) {
        message.setFlag(Flags.Flag.DELETED, true);
        log.info("Deleted email Subject : " + message.getSubject());
      }
     } catch (MessagingException e) {
        log.error("Error while connecting to email store ", e);
        throw new MessagingException("Error while connecting to email store ", e);
     } finally {
       if (inbox != null) {
         inbox.close(true);
       }
       if (store != null) {
         store.close();
       }
  }

No comments:

Post a Comment