Rev 3373 | Rev 4523 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.util;import in.shop2020.model.v1.user.UserContextException;import java.io.IOException;import java.text.ParseException;import java.util.Date;import java.util.Properties;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.NoSuchProviderException;import javax.mail.Session;import javax.mail.Store;import javax.mail.search.ComparisonTerm;import javax.mail.search.ReceivedDateTerm;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.thrift.TException;/*** @author mandeep**/public class EmailReader {private static final Log log = LogFactory.getLog(EmailReader.class);Store store;private EmailReader(String username, String password) {Properties props = System.getProperties();props.setProperty("mail.store.protocol", "imaps");try {Session session = Session.getDefaultInstance(props, null);store = session.getStore("imaps");store.connect("imap.gmail.com", username, password);} catch (NoSuchProviderException e) {log.error("Error getting store", e);} catch (MessagingException e) {log.error("Error while connecting", e);}}public Folder getInboxFolder() throws MessagingException {return store.getFolder("inbox");}/*** @param args* @throws MessagingException* @throws ParseException* @throws TException* @throws UserContextException*/public static void main(String[] args) throws MessagingException,ParseException, TException, UserContextException {Folder inbox = new EmailReader("help@shop2020.in", "5h0p2o2o").getInboxFolder();inbox.open(Folder.READ_ONLY);CRMEmailProcessor crmEmailProcessor = new CRMEmailProcessor();Date lastProcessedEmailTimestamp = crmEmailProcessor.getLastProcessedTimestamp();Date lastReceivedEmailTimestamp = lastProcessedEmailTimestamp;for (Message message : inbox.search(new ReceivedDateTerm(ComparisonTerm.GE, lastProcessedEmailTimestamp))){try {// Skipping mails received before the last processed timestampif (!lastProcessedEmailTimestamp.before(message.getReceivedDate())) {continue;}log.info("Processing message with Receive date: " + message.getReceivedDate() + " and subject: " + message.getSubject());crmEmailProcessor.processEmail(message);lastReceivedEmailTimestamp = message.getReceivedDate();} catch (IOException e) {log.error("Could not process message: " + message, e);} catch (TException e) {log.error("Could not process message: " + message, e);} catch (MessagingException e) {log.error("Could not process message: " + message, e);}}crmEmailProcessor.updateLastProcessedTimestamp(lastReceivedEmailTimestamp);}}