| 3339 |
mandeep.dh |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.util;
|
|
|
5 |
|
|
|
6 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
7 |
|
|
|
8 |
import java.io.IOException;
|
|
|
9 |
import java.text.ParseException;
|
|
|
10 |
import java.util.Date;
|
|
|
11 |
import java.util.Properties;
|
|
|
12 |
|
|
|
13 |
import javax.mail.Folder;
|
|
|
14 |
import javax.mail.Message;
|
|
|
15 |
import javax.mail.MessagingException;
|
|
|
16 |
import javax.mail.NoSuchProviderException;
|
|
|
17 |
import javax.mail.Session;
|
|
|
18 |
import javax.mail.Store;
|
|
|
19 |
import javax.mail.search.ComparisonTerm;
|
|
|
20 |
import javax.mail.search.ReceivedDateTerm;
|
|
|
21 |
|
|
|
22 |
import org.apache.commons.logging.Log;
|
|
|
23 |
import org.apache.commons.logging.LogFactory;
|
|
|
24 |
import org.apache.thrift.TException;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* @author mandeep
|
|
|
28 |
*
|
|
|
29 |
*/
|
|
|
30 |
public class EmailReader {
|
|
|
31 |
private static final Log log = LogFactory.getLog(EmailReader.class);
|
|
|
32 |
|
|
|
33 |
Store store;
|
|
|
34 |
|
|
|
35 |
private EmailReader(String username, String password) {
|
|
|
36 |
Properties props = System.getProperties();
|
|
|
37 |
props.setProperty("mail.store.protocol", "imaps");
|
|
|
38 |
try {
|
|
|
39 |
Session session = Session.getDefaultInstance(props, null);
|
|
|
40 |
store = session.getStore("imaps");
|
|
|
41 |
store.connect("imap.gmail.com", username, password);
|
|
|
42 |
} catch (NoSuchProviderException e) {
|
|
|
43 |
log.error("Error getting store", e);
|
|
|
44 |
} catch (MessagingException e) {
|
|
|
45 |
log.error("Error while connecting", e);
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public Folder getInboxFolder() throws MessagingException {
|
|
|
50 |
return store.getFolder("inbox");
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* @param args
|
|
|
55 |
* @throws MessagingException
|
|
|
56 |
* @throws ParseException
|
|
|
57 |
* @throws TException
|
|
|
58 |
* @throws UserContextException
|
|
|
59 |
*/
|
|
|
60 |
public static void main(String[] args) throws MessagingException,
|
|
|
61 |
ParseException, TException, UserContextException {
|
|
|
62 |
Folder inbox = new EmailReader("help@shop2020.in", "5h0p2o2o").getInboxFolder();
|
|
|
63 |
inbox.open(Folder.READ_ONLY);
|
|
|
64 |
CRMEmailProcessor crmEmailProcessor = new CRMEmailProcessor();
|
| 3368 |
mandeep.dh |
65 |
Date lastProcessedEmailTimestamp = crmEmailProcessor.getLastProcessedTimestamp();
|
|
|
66 |
Date lastReceivedEmailTimestamp = lastProcessedEmailTimestamp;
|
|
|
67 |
|
| 3339 |
mandeep.dh |
68 |
for (Message message : inbox.search(new ReceivedDateTerm(
|
| 3368 |
mandeep.dh |
69 |
ComparisonTerm.GE, lastProcessedEmailTimestamp)))
|
| 3339 |
mandeep.dh |
70 |
{
|
|
|
71 |
try {
|
| 3368 |
mandeep.dh |
72 |
// Skipping mails received before the last processed timestamp
|
|
|
73 |
if (!lastProcessedEmailTimestamp.before(message.getReceivedDate())) {
|
|
|
74 |
continue;
|
|
|
75 |
}
|
|
|
76 |
|
| 3373 |
mandeep.dh |
77 |
crmEmailProcessor.processEmail(message);
|
| 3339 |
mandeep.dh |
78 |
lastReceivedEmailTimestamp = message.getReceivedDate();
|
|
|
79 |
} catch (IOException e) {
|
|
|
80 |
log.error("Could not process message: " + message, e);
|
|
|
81 |
} catch (TException e) {
|
|
|
82 |
log.error("Could not process message: " + message, e);
|
|
|
83 |
} catch (MessagingException e) {
|
|
|
84 |
log.error("Could not process message: " + message, e);
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
crmEmailProcessor.updateLastProcessedTimestamp(lastReceivedEmailTimestamp);
|
|
|
89 |
}
|
|
|
90 |
}
|